<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/Frog_16px_1177822_easyicon.net.ico">
  <link rel="mask-icon" href="/images/Frog_32px_1177822_easyicon.net.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"hxy1997.xyz","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":true,"nav":null,"activeClass":"valine"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"输入关键字","hits_empty":"没有找到与「${query}」相关搜索","hits_stats":"${hits} 条相关记录，共耗时 ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.json"};
  </script>

  <meta name="description" content="使用 Vue搭建的电商管理后台项目，手把手地指引新人每一步的过程，项目调用了别人提供的后端接口。笔记我是重新梳理了一遍，只有动手做才能发现自己踩过的坑，加以改进。为了方便，我最终选择将自己学习搭建的Vue电商项目托管至Gitee Pages和GitHub Pages，实现项目的正式上线 Vue电商项目(Gitee Pages)和Vue电商项目(GitHub Pages)，其中GitHub Page">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue电商管理后台项目">
<meta property="og:url" content="https://hxy1997.xyz/2020/10/21/Vue%E7%94%B5%E5%95%86%E7%AE%A1%E7%90%86%E5%90%8E%E5%8F%B0%E9%A1%B9%E7%9B%AE/index.html">
<meta property="og:site_name" content="hxy的博客">
<meta property="og:description" content="使用 Vue搭建的电商管理后台项目，手把手地指引新人每一步的过程，项目调用了别人提供的后端接口。笔记我是重新梳理了一遍，只有动手做才能发现自己踩过的坑，加以改进。为了方便，我最终选择将自己学习搭建的Vue电商项目托管至Gitee Pages和GitHub Pages，实现项目的正式上线 Vue电商项目(Gitee Pages)和Vue电商项目(GitHub Pages)，其中GitHub Page">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145540.jpg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145548.jpg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145555.jpg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145602.jpg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145609.jpg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145617.jpg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145623.jpg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145634.jpg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145644.jpg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145650.jpg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145655.jpg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145701.jpg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145709.jpg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145719.jpg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145723.jpg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145732.jpg">
<meta property="article:published_time" content="2020-10-21T04:02:39.000Z">
<meta property="article:modified_time" content="2021-04-18T06:58:27.819Z">
<meta property="article:author" content="hxy">
<meta property="article:tag" content="Vue">
<meta property="article:tag" content="实战">
<meta property="article:tag" content="电商管理后台">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145540.jpg">

<link rel="canonical" href="https://hxy1997.xyz/2020/10/21/Vue%E7%94%B5%E5%95%86%E7%AE%A1%E7%90%86%E5%90%8E%E5%8F%B0%E9%A1%B9%E7%9B%AE/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Vue电商管理后台项目 | hxy的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="hxy的博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">hxy的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Mia san Mia!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/huxingyi1997" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://hxy1997.xyz/2020/10/21/Vue%E7%94%B5%E5%95%86%E7%AE%A1%E7%90%86%E5%90%8E%E5%8F%B0%E9%A1%B9%E7%9B%AE/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/Robben.gif">
      <meta itemprop="name" content="hxy">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="hxy的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Vue电商管理后台项目
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-10-21 12:02:39" itemprop="dateCreated datePublished" datetime="2020-10-21T12:02:39+08:00">2020-10-21</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-04-18 14:58:27" itemprop="dateModified" datetime="2021-04-18T14:58:27+08:00">2021-04-18</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/web%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">web前端</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="热度" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">热度：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/10/21/Vue%E7%94%B5%E5%95%86%E7%AE%A1%E7%90%86%E5%90%8E%E5%8F%B0%E9%A1%B9%E7%9B%AE/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/10/21/Vue%E7%94%B5%E5%95%86%E7%AE%A1%E7%90%86%E5%90%8E%E5%8F%B0%E9%A1%B9%E7%9B%AE/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>使用 Vue搭建的电商管理后台项目，手把手地指引新人每一步的过程，项目调用了别人提供的后端接口。笔记我是重新梳理了一遍，只有动手做才能发现自己踩过的坑，加以改进。为了方便，我最终选择将自己学习搭建的Vue电商项目托管至Gitee Pages和GitHub Pages，实现项目的正式上线 <a target="_blank" rel="noopener" href="https://hxy1997.gitee.io/vue_shops/">Vue电商项目(Gitee Pages)</a>和<a href="https://hxy1997.xyz/vue_shops/">Vue电商项目(GitHub Pages)</a>，其中GitHub Pages采用的是我自己在阿里云配置的域名，所以和博客是同一个域名。</p>
<span id="more"></span>

<h1 id="1-电商业务概述"><a href="#1-电商业务概述" class="headerlink" title="1.电商业务概述"></a>1.电商业务概述</h1><p>客户使用的业务服务：PC端，小程序，移动web，移动app<br>管理员使用的业务服务：PC后台管理端。<br>PC后台管理端的功能：管理用户账号（登录，退出，用户管理，权限管理），商品管理（商品分类，分类参数，商品信息，订单），数据统计<br>电商后台管理系统采用前后端分离的开发模式<br>前端项目是基于Vue的SPA（单页应用程序）项目</p>
<p>前端技术栈:Vue,Vue-Router,Element-UI,Axios,Echarts<br>后端技术栈：Node.js,Express,Jwt(模拟session),Mysql,Sequelize(操作数据库的框架)</p>
<h1 id="2-项目初始化"><a href="#2-项目初始化" class="headerlink" title="2.项目初始化"></a>2.项目初始化</h1><h2 id="2-1-安装Vue脚手架"><a href="#2-1-安装Vue脚手架" class="headerlink" title="2.1 安装Vue脚手架"></a>2.1 安装Vue脚手架</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i @vue&#x2F;cli -g</span><br></pre></td></tr></table></figure>

<h2 id="2-2-通过脚手架创建项目"><a href="#2-2-通过脚手架创建项目" class="headerlink" title="2.2 通过脚手架创建项目"></a>2.2 通过脚手架创建项目</h2><p>可视化操作 </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vue ui</span><br></pre></td></tr></table></figure>

<h2 id="2-3-配置路由"><a href="#2-3-配置路由" class="headerlink" title="2.3 配置路由"></a>2.3 配置路由</h2><h2 id="2-4-配置Element-UI-在插件中安装，搜索vue-cli-plugin-element"><a href="#2-4-配置Element-UI-在插件中安装，搜索vue-cli-plugin-element" class="headerlink" title="2.4 配置Element-UI:在插件中安装，搜索vue-cli-plugin-element"></a>2.4 配置Element-UI:在插件中安装，搜索vue-cli-plugin-element</h2><h2 id="2-5-配置Axios：在依赖中安装-搜索axios-运行依赖"><a href="#2-5-配置Axios：在依赖中安装-搜索axios-运行依赖" class="headerlink" title="2.5 配置Axios：在依赖中安装,搜索axios(运行依赖)"></a>2.5 配置Axios：在依赖中安装,搜索axios(运行依赖)</h2><h2 id="2-6-初始化git仓库"><a href="#2-6-初始化git仓库" class="headerlink" title="2.6 初始化git仓库"></a>2.6 初始化git仓库</h2><h2 id="2-7-将本地项目托管到github或者码云中"><a href="#2-7-将本地项目托管到github或者码云中" class="headerlink" title="2.7 将本地项目托管到github或者码云中"></a>2.7 将本地项目托管到github或者码云中</h2><h1 id="3-码云相关操作"><a href="#3-码云相关操作" class="headerlink" title="3.码云相关操作"></a>3.码云相关操作</h1><h2 id="3-1-注册登录码云账号"><a href="#3-1-注册登录码云账号" class="headerlink" title="3.1 注册登录码云账号"></a>3.1 注册登录码云账号</h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145540.jpg"></p>
<h2 id="3-2-安装git"><a href="#3-2-安装git" class="headerlink" title="3.2 安装git"></a>3.2 安装git</h2><p>​    在Windows上使用Git，可以从Git官网直接下载安装程序进行安装。<br>​    测试命令：git –version</p>
<h2 id="3-3-点击网站右上角“登录”，登录码云，并进行账号设置"><a href="#3-3-点击网站右上角“登录”，登录码云，并进行账号设置" class="headerlink" title="3.3 点击网站右上角“登录”，登录码云，并进行账号设置"></a>3.3 点击网站右上角“登录”，登录码云，并进行账号设置</h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145548.jpg"></p>
<h2 id="3-4-在本地创建公钥："><a href="#3-4-在本地创建公钥：" class="headerlink" title="3.4 在本地创建公钥："></a>3.4 在本地创建公钥：</h2><p>在终端运行：ssh-keygen -t rsa -C “<a href="mailto:&#x78;&#x78;&#120;&#x40;&#x78;&#x78;&#120;&#46;&#x63;&#x6f;&#109;">&#x78;&#x78;&#120;&#x40;&#x78;&#x78;&#120;&#46;&#x63;&#x6f;&#109;</a>“<br><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145555.jpg"></p>
<h2 id="3-5-找到公钥地址："><a href="#3-5-找到公钥地址：" class="headerlink" title="3.5 找到公钥地址："></a>3.5 找到公钥地址：</h2><p>Your identification has been saved in /c/Users/My/.ssh/id_rsa.<br>Your public key has been saved in /c/Users/My/.ssh/id_rsa.pub.<br>当我们创建公钥完毕之后，请注意打印出来的信息“Your public key has been saved in”<br>/c/Users/My/.ssh/id_rsa.pub : c盘下面的Users下面的My下面的.ssh下面的id_rsa.pub就是我们创建好的公钥了</p>
<p>E.打开id_rsa.pub文件，复制文件中的所有代码，点击码云中的SSH公钥，将生成的公钥复制到公钥中<br><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145602.jpg"></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145609.jpg"></p>
<h2 id="3-6-测试公钥：打开终端，输入命令"><a href="#3-6-测试公钥：打开终端，输入命令" class="headerlink" title="3.6 测试公钥：打开终端，输入命令"></a>3.6 测试公钥：打开终端，输入命令</h2><p>ssh -T <a href="mailto:&#x67;&#105;&#x74;&#x40;&#x67;&#x69;&#x74;&#x65;&#x65;&#x2e;&#99;&#x6f;&#x6d;">&#x67;&#105;&#x74;&#x40;&#x67;&#x69;&#x74;&#x65;&#x65;&#x2e;&#99;&#x6f;&#x6d;</a><br><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145617.jpg"></p>
<h2 id="3-7-将本地代码托管到码云中"><a href="#3-7-将本地代码托管到码云中" class="headerlink" title="3.7 将本地代码托管到码云中"></a>3.7 将本地代码托管到码云中</h2><p>点击码云右上角的+号-&gt;新建仓库<br><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145623.jpg"><br><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145634.jpg"></p>
<h2 id="3-8-进行git配置："><a href="#3-8-进行git配置：" class="headerlink" title="3.8 进行git配置："></a>3.8 进行git配置：</h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145644.jpg"><br>打开项目所在位置的终端，进行git仓库关联<br><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145650.jpg"></p>
<h1 id="4-配置后台项目"><a href="#4-配置后台项目" class="headerlink" title="4.配置后台项目"></a>4.配置后台项目</h1><h2 id="4-1-安装phpStudy并导入mysql数据库数据"><a href="#4-1-安装phpStudy并导入mysql数据库数据" class="headerlink" title="4.1 安装phpStudy并导入mysql数据库数据"></a>4.1 安装phpStudy并导入mysql数据库数据</h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145655.jpg"></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145701.jpg"></p>
<h2 id="4-2-安装nodeJS，配置后台项目-从终端打开后台项目vue-api-server"><a href="#4-2-安装nodeJS，配置后台项目-从终端打开后台项目vue-api-server" class="headerlink" title="4.2 安装nodeJS，配置后台项目,从终端打开后台项目vue_api_server"></a>4.2 安装nodeJS，配置后台项目,从终端打开后台项目vue_api_server</h2><p>然后在终端中输入命令安装项目依赖包：npm install</p>
<h2 id="4-3-使用postman测试api接口"><a href="#4-3-使用postman测试api接口" class="headerlink" title="4.3 使用postman测试api接口"></a>4.3 使用postman测试api接口</h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145709.jpg"></p>
<h1 id="5-实现登录功能"><a href="#5-实现登录功能" class="headerlink" title="5.实现登录功能"></a>5.实现登录功能</h1><h2 id="5-1-登录状态保持"><a href="#5-1-登录状态保持" class="headerlink" title="5.1 登录状态保持"></a>5.1 登录状态保持</h2><p>如果服务器和客户端是同源，建议可以使用cookie或者session来保持登录状态<br>如果客户端和服务器跨域了，建议使用token进行维持登录状态。</p>
<h2 id="5-2-登录逻辑："><a href="#5-2-登录逻辑：" class="headerlink" title="5.2 登录逻辑："></a>5.2 登录逻辑：</h2><p>在登录页面输入账号和密码进行登录，将数据发送给服务器<br>服务器返回登录的结果，登录成功则返回数据中带有token<br>客户端得到token并进行保存，后续的请求都需要将此token发送给服务器，服务器会验证token以保证用户身份。</p>
<h2 id="5-3-添加新分支login，在login分支中开发当前项目vue-shop："><a href="#5-3-添加新分支login，在login分支中开发当前项目vue-shop：" class="headerlink" title="5.3 添加新分支login，在login分支中开发当前项目vue_shop："></a>5.3 添加新分支login，在login分支中开发当前项目vue_shop：</h2><p>打开vue_shop终端，使用git status确定当前项目状态。<br>确定当前工作目录是干净的之后，创建一个分支进行开发，开发完毕之后将其合并到master<br>git checkout -b login<br>然后查看新创建的分支：git branch<br>确定我们正在使用login分支进行开发<br><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145719.jpg"></p>
<p>然后执行vue ui命令打开ui界面，然后运行serve，运行app查看当前项目效果<br><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145723.jpg"></p>
<p>发现现在是一个默认页面，我们需要进行更改，打开项目的src目录，点击main.js文件（入口文件）</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">import Vue from &#39;vue&#39;</span><br><span class="line">import App from &#39;.&#x2F;App.vue&#39;</span><br><span class="line">import router from &#39;.&#x2F;router&#39;</span><br><span class="line">import &#39;.&#x2F;plugins&#x2F;element.js&#39;</span><br><span class="line"></span><br><span class="line">Vue.config.productionTip &#x3D; false</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">new Vue(&#123;</span><br><span class="line">  router,</span><br><span class="line">  render: h &#x3D;&gt; h(App)</span><br><span class="line">&#125;).$mount(&#39;#app&#39;)</span><br></pre></td></tr></table></figure>
<p>再打开App.vue(根组件)，将根组件的内容进行操作梳理(template中留下根节点，script中留下默认导出，去掉组件，style中去掉所有样式)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div id&#x3D;&quot;app&quot;&gt;</span><br><span class="line">    &lt;router-view&gt;&lt;&#x2F;router-view&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  name: &#39;app&#39;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">&lt;&#x2F;style&gt;</span><br></pre></td></tr></table></figure>
<p>再打开router.js(路由)，将routes数组中的路由规则清除，然后将views删除，将components中的helloworld.vue删除</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">import Vue from &#39;vue&#39;</span><br><span class="line">import Router from &#39;vue-router&#39;</span><br><span class="line"></span><br><span class="line">Vue.use(Router)</span><br><span class="line"></span><br><span class="line">export default new Router(&#123;</span><br><span class="line">  routes: [</span><br><span class="line">    </span><br><span class="line">  ]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>在components文件夹中新建Login.vue组件,添加template，script，style标签,style标签中的scoped可以防止组件之间的样式冲突，没有scoped则样式是全局的</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;div class&#x3D;&quot;login_container&quot;&gt;</span><br><span class="line">        </span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  </span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style lang&#x3D;&quot;less&quot; scoped&gt;</span><br><span class="line">.login_container &#123;</span><br><span class="line">  background-color: #2b5b6b;</span><br><span class="line">  height: 100%;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;style&gt;</span><br></pre></td></tr></table></figure>
<p>在router.js中导入组件并设置规则<br>在App.vue中添加路由占位符</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">const router &#x3D; new Router(&#123;</span><br><span class="line">  routes: [</span><br><span class="line">    &#123; path: &#39;&#x2F;&#39;, redirect: &#39;&#x2F;login&#39; &#125;,</span><br><span class="line">    &#123; path: &#39;&#x2F;login&#39;, component: Login &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>当我们给Login.vue中的内容添加样式的时候，会报错“缺少less-loader”，需要配置less加载器（开发依赖），安装less(开发依赖)<br><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418145732.jpg"><br>然后需要添加公共样式，在assets文件夹下面添加css文件夹，创建global.css文件,添加全局样式</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;* 全局样式表 *&#x2F;</span><br><span class="line">html,body,#app&#123;</span><br><span class="line">    width: 100%;</span><br><span class="line">    height: 100%;</span><br><span class="line">    margin: 0;</span><br><span class="line">    padding: 0; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在main.js中导入global.css，使得全局样式生效 import “./assets/css/global.css”<br>然后Login.vue中的根元素也需要设置撑满全屏（height:100%）<br>最终Login.vue文件中的代码如下</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;div class&#x3D;&quot;login_container&quot;&gt;</span><br><span class="line">        &lt;!-- 登录盒子  --&gt;</span><br><span class="line">        &lt;div class&#x3D;&quot;login_box&quot;&gt;</span><br><span class="line">            &lt;!-- 头像 --&gt;</span><br><span class="line">            &lt;div class&#x3D;&quot;avatar_box&quot;&gt;</span><br><span class="line">                &lt;img src&#x3D;&quot;..&#x2F;assets&#x2F;logo.png&quot; alt&#x3D;&quot;&quot;&gt;</span><br><span class="line">            &lt;&#x2F;div&gt;</span><br><span class="line">            &lt;!-- 登录表单 --&gt;</span><br><span class="line">            &lt;el-form :model&#x3D;&quot;loginForm&quot; ref&#x3D;&quot;LoginFormRef&quot; :rules&#x3D;&quot;loginFormRules&quot; label-width&#x3D;&quot;0px&quot; class&#x3D;&quot;login_form&quot;&gt;</span><br><span class="line">                &lt;!-- 用户名 --&gt;</span><br><span class="line">                &lt;el-form-item prop&#x3D;&quot;username&quot;&gt;</span><br><span class="line">                    &lt;el-input v-model&#x3D;&quot;loginForm.username&quot; prefix-icon&#x3D;&quot;iconfont icon-user&quot; &gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">                &lt;&#x2F;el-form-item&gt; </span><br><span class="line">                &lt;!-- 密码 --&gt;</span><br><span class="line">                &lt;el-form-item prop&#x3D;&quot;password&quot;&gt;</span><br><span class="line">                    &lt;el-input type&#x3D;&quot;password&quot; v-model&#x3D;&quot;loginForm.password&quot; prefix-icon&#x3D;&quot;iconfont icon-3702mima&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">                &lt;&#x2F;el-form-item&gt; </span><br><span class="line">                &lt;!-- 按钮 --&gt;</span><br><span class="line">                &lt;el-form-item class&#x3D;&quot;btns&quot;&gt;</span><br><span class="line">                    &lt;el-button type&#x3D;&quot;primary&quot; @click&#x3D;&quot;login&quot;&gt;登录&lt;&#x2F;el-button&gt;</span><br><span class="line">                    &lt;el-button type&#x3D;&quot;info&quot; @click&#x3D;&quot;resetLoginForm&quot;&gt;重置&lt;&#x2F;el-button&gt;</span><br><span class="line">                &lt;&#x2F;el-form-item&gt; </span><br><span class="line">            &lt;&#x2F;el-form&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      &#x2F;&#x2F; 数据绑定</span><br><span class="line">      loginForm: &#123;</span><br><span class="line">        username: &#39;admin&#39;,</span><br><span class="line">        password: &#39;123456&#39;</span><br><span class="line">      &#125;,</span><br><span class="line">      &#x2F;&#x2F; 表单验证规则</span><br><span class="line">      loginFormRules: &#123;</span><br><span class="line">        username: [</span><br><span class="line">          &#123; required: true, message: &#39;请输入登录名&#39;, trigger: &#39;blur&#39; &#125;,</span><br><span class="line">          &#123;</span><br><span class="line">            min: 3,</span><br><span class="line">            max: 10,</span><br><span class="line">            message: &#39;登录名长度在 3 到 10 个字符&#39;,</span><br><span class="line">            trigger: &#39;blur&#39;</span><br><span class="line">          &#125;</span><br><span class="line">        ],</span><br><span class="line">        password: [</span><br><span class="line">          &#123; required: true, message: &#39;请输入密码&#39;, trigger: &#39;blur&#39; &#125;,</span><br><span class="line">          &#123;</span><br><span class="line">            min: 6,</span><br><span class="line">            max: 15,</span><br><span class="line">            message: &#39;密码长度在 6 到 15 个字符&#39;,</span><br><span class="line">            trigger: &#39;blur&#39;</span><br><span class="line">          &#125;</span><br><span class="line">        ]</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  &#x2F;&#x2F; 添加行为，</span><br><span class="line">  methods: &#123;</span><br><span class="line">    &#x2F;&#x2F; 添加表单重置方法</span><br><span class="line">    resetLoginForm() &#123;</span><br><span class="line">      &#x2F;&#x2F; this&#x3D;&gt;当前组件对象，其中的属性$refs包含了设置的表单ref</span><br><span class="line">      &#x2F;&#x2F; console.log(this)</span><br><span class="line">      this.$refs.LoginFormRef.resetFields()</span><br><span class="line">    &#125;,</span><br><span class="line">    login() &#123;</span><br><span class="line">      &#x2F;&#x2F; 点击登录的时候先调用validate方法验证表单内容是否有误</span><br><span class="line">      this.$refs.LoginFormRef.validate(async valid &#x3D;&gt; &#123;</span><br><span class="line">        console.log(this.loginFormRules)</span><br><span class="line">        &#x2F;&#x2F; 如果valid参数为true则验证通过</span><br><span class="line">        if (!valid) &#123;</span><br><span class="line">          return</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        &#x2F;&#x2F; 发送请求进行登录</span><br><span class="line">        const &#123; data: res &#125; &#x3D; await this.$http.post(&#39;login&#39;, this.loginForm)</span><br><span class="line">        &#x2F;&#x2F;   console.log(res);</span><br><span class="line">        if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">          return this.$message.error(&#39;登录失败:&#39; + res.meta.msg) &#x2F;&#x2F;console.log(&quot;登录失败:&quot;+res.meta.msg)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        this.$message.success(&#39;登录成功&#39;)</span><br><span class="line">        console.log(res)</span><br><span class="line">        &#x2F;&#x2F; 保存token</span><br><span class="line">        window.sessionStorage.setItem(&#39;token&#39;, res.data.token)</span><br><span class="line">        &#x2F;&#x2F; 导航至&#x2F;home</span><br><span class="line">        this.$router.push(&#39;&#x2F;home&#39;)</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style lang&#x3D;&quot;less&quot; scoped&gt;</span><br><span class="line">.login_container &#123;</span><br><span class="line">  background-color: #2b5b6b;</span><br><span class="line">  height: 100%;</span><br><span class="line">&#125;</span><br><span class="line">.login_box &#123;</span><br><span class="line">  width: 450px;</span><br><span class="line">  height: 300px;</span><br><span class="line">  background: #fff;</span><br><span class="line">  border-radius: 3px;</span><br><span class="line">  position: absolute;</span><br><span class="line">  left: 50%;</span><br><span class="line">  top: 50%;</span><br><span class="line">  transform: translate(-50%, -50%);</span><br><span class="line">  .avatar_box &#123;</span><br><span class="line">    height: 130px;</span><br><span class="line">    width: 130px;</span><br><span class="line">    border: 1px solid #eee;</span><br><span class="line">    border-radius: 50%;</span><br><span class="line">    padding: 10px;</span><br><span class="line">    box-shadow: 0 0 10px #ddd;</span><br><span class="line">    position: absolute;</span><br><span class="line">    left: 50%;</span><br><span class="line">    transform: translate(-50%, -50%);</span><br><span class="line">    background-color: #fff;</span><br><span class="line">    img &#123;</span><br><span class="line">      width: 100%;</span><br><span class="line">      height: 100%;</span><br><span class="line">      border-radius: 50%;</span><br><span class="line">      background-color: #eee;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">.login_form &#123;</span><br><span class="line">  position: absolute;</span><br><span class="line">  bottom: 0;</span><br><span class="line">  width: 100%;</span><br><span class="line">  padding: 0 20px;</span><br><span class="line">  box-sizing: border-box;</span><br><span class="line">&#125;</span><br><span class="line">.btns &#123;</span><br><span class="line">  display: flex;</span><br><span class="line">  justify-content: flex-end;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;style&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>其中我们有用到一些未导入的内容，需要进行进一步处理：</p>
<h3 id="5-3-1-添加element-ui的表单组件"><a href="#5-3-1-添加element-ui的表单组件" class="headerlink" title="5.3.1 添加element-ui的表单组件"></a>5.3.1 添加element-ui的表单组件</h3><p>在plugins文件夹中打开element.js文件，进行elementui的按需导入<br>import Vue from ‘vue’<br>import { Button } from ‘element-ui’<br>import { Form, FormItem } from ‘element-ui’<br>import { Input } from ‘element-ui’</p>
<p>Vue.use(Button)<br>Vue.use(Form)<br>Vue.use(FormItem)<br>Vue.use(Input)</p>
<h3 id="5-3-2-添加第三方字体"><a href="#5-3-2-添加第三方字体" class="headerlink" title="5.3.2 添加第三方字体"></a>5.3.2 添加第三方字体</h3><p>复制素材中的fonts文件夹到assets中,在入口文件main.js中导入import ‘./assets/fonts/iconfont.css’<br>然后直接在 <el-input prefix-icon="iconfont icon-3702mima"></el-input><br>接着添加登录盒子</p>
<h3 id="5-3-3-添加表单验证的步骤"><a href="#5-3-3-添加表单验证的步骤" class="headerlink" title="5.3.3 添加表单验证的步骤"></a>5.3.3 添加表单验证的步骤</h3><h4 id="1-给添加属性-rules-”rules”，rules是一堆验证规则，定义在script、中"><a href="#1-给添加属性-rules-”rules”，rules是一堆验证规则，定义在script、中" class="headerlink" title="1).给添加属性:rules=”rules”，rules是一堆验证规则，定义在script、中"></a>1).给<el-form>添加属性:rules=”rules”，rules是一堆验证规则，定义在script、中</h4><h4 id="2-在script中添加rules：export-default-data-return-……-rules"><a href="#2-在script中添加rules：export-default-data-return-……-rules" class="headerlink" title="2).在script中添加rules：export default{ data(){return{……, rules: {"></a>2).在script中添加rules：export default{ data(){return{……, rules: {</h4><p>​          name: [<br>​            { required: true, message: ‘请输入活动名称’, trigger: ‘blur’ },<br>​            { min: 3, max: 5, message: ‘长度在 3 到 5 个字符’, trigger: ‘blur’ }<br>​          ],<br>​          region: [<br>​            { required: true, message: ‘请选择活动区域’, trigger: ‘change’ }<br>​          ]<br>}……</p>
<h4 id="3-通过的prop属性设置验证规则"><a href="#3-通过的prop属性设置验证规则" class="headerlink" title="3).通过的prop属性设置验证规则"></a>3).通过<el-form-item>的prop属性设置验证规则<el-form-item label="活动名称" prop="name"></h4><h4 id="4-导入axios以发送ajax请求"><a href="#4-导入axios以发送ajax请求" class="headerlink" title="4.导入axios以发送ajax请求"></a>4.导入axios以发送ajax请求</h4><p>打开main.js，import axios from ‘axios’;<br>设置请求的根路径：axios.defaults.baseURL = ‘<a target="_blank" rel="noopener" href="http://127.0.0.1:8888/api/private/v1/&#39;">http://127.0.0.1:8888/api/private/v1/&#39;</a>;<br>挂载axios：Vue.prototype.$http = axios;</p>
<h4 id="5-配置弹窗提示："><a href="#5-配置弹窗提示：" class="headerlink" title="5.配置弹窗提示："></a>5.配置弹窗提示：</h4><p>在plugins文件夹中打开element.js文件，进行elementui的按需导入<br>import {Message} from ‘element-ui’<br>进行全局挂载：Vue.prototype.$message = Message;<br>在login.vue组件中编写弹窗代码：this.$message.error(‘登录失败’)</p>
<h2 id="5-4-登录成功之后的操作"><a href="#5-4-登录成功之后的操作" class="headerlink" title="5.4 登录成功之后的操作"></a>5.4 登录成功之后的操作</h2><h3 id="5-4-1-登录成功之后，需要将后台返回的token保存到sessionStorage中"><a href="#5-4-1-登录成功之后，需要将后台返回的token保存到sessionStorage中" class="headerlink" title="5.4.1 登录成功之后，需要将后台返回的token保存到sessionStorage中"></a>5.4.1 登录成功之后，需要将后台返回的token保存到sessionStorage中</h3><p>操作完毕之后，需要跳转到/home</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">login() &#123;</span><br><span class="line">      &#x2F;&#x2F;点击登录的时候先调用validate方法验证表单内容是否有误</span><br><span class="line">      this.$refs.LoginFormRef.validate(async valid &#x3D;&gt; &#123;</span><br><span class="line">        console.log(this.loginFormRules)</span><br><span class="line">        &#x2F;&#x2F;如果valid参数为true则验证通过</span><br><span class="line">        if (!valid) &#123;</span><br><span class="line">          return</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        &#x2F;&#x2F; 发送请求进行登录</span><br><span class="line">        const &#123; data: res &#125; &#x3D; await this.$http.post(&#39;login&#39;, this.loginForm)</span><br><span class="line">        &#x2F;&#x2F;   console.log(res);</span><br><span class="line">        if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">          return this.$message.error(&#39;登录失败:&#39; + res.meta.msg) &#x2F;&#x2F; console.log(&quot;登录失败:&quot;+res.meta.msg)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        this.$message.success(&#39;登录成功&#39;)</span><br><span class="line">        console.log(res)</span><br><span class="line">        &#x2F;&#x2F; 保存token</span><br><span class="line">        window.sessionStorage.setItem(&#39;token&#39;, res.data.token)</span><br><span class="line">        &#x2F;&#x2F; 导航至&#x2F;home</span><br><span class="line">        this.$router.push(&#39;&#x2F;home&#39;)</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>添加一个组件Home.vue，并为之添加规则</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        this is home</span><br><span class="line">        &lt;el-button type&#x3D;&quot;info&quot; @click&#x3D;&quot;logout&quot;&gt; 退出 &lt;&#x2F;el-button&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  methods: &#123;</span><br><span class="line">    logout() &#123;</span><br><span class="line">      window.sessionStorage.clear()</span><br><span class="line">      this.$router.push(&#39;&#x2F;login&#39;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style lang&#x3D;&#39;less&#39; scoped&gt;</span><br><span class="line">&lt;&#x2F;style&gt;</span><br></pre></td></tr></table></figure>
<p>添加路由规则</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">const router &#x3D; new Router(&#123;</span><br><span class="line">  routes: [</span><br><span class="line">    &#123; path: &#39;&#x2F;&#39;, redirect: &#39;&#x2F;login&#39; &#125;,</span><br><span class="line">    &#123; path: &#39;&#x2F;login&#39;, component: Login &#125;,</span><br><span class="line">    &#123; path: &#39;&#x2F;home&#39;, component: Home &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>添加路由守卫<br>如果用户没有登录，不能访问/home,如果用户通过url地址直接访问，则强制跳转到登录页面<br>打开router.js</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">import Vue from &#39;vue&#39;</span><br><span class="line">import Router from &#39;vue-router&#39;</span><br><span class="line">import Login from &#39;.&#x2F;components&#x2F;Login.vue&#39;</span><br><span class="line">import Home from &#39;.&#x2F;components&#x2F;Home.vue&#39;</span><br><span class="line"></span><br><span class="line">Vue.use(Router)</span><br><span class="line"></span><br><span class="line">const router &#x3D; new Router(&#123;</span><br><span class="line">  routes: [</span><br><span class="line">    &#123; path:&#39;&#x2F;&#39;, redirect:&#39;&#x2F;login&#39;&#125;,</span><br><span class="line">    &#123; path:&#39;&#x2F;login&#39; , component:Login &#125;,</span><br><span class="line">    &#123; path:&#39;&#x2F;home&#39; , component:Home&#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 挂载路由导航守卫,to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作</span><br><span class="line">router.beforeEach((to, from, next)&#x3D;&gt;&#123; </span><br><span class="line">  if(to.path &#x3D;&#x3D;&#x3D; &#39;&#x2F;login&#39;)</span><br><span class="line">    return next();</span><br><span class="line">  </span><br><span class="line">  &#x2F;&#x2F;获取token</span><br><span class="line">  const tokenStr &#x3D; window.sessionStorage.getItem(&#39;token&#39;);</span><br><span class="line"></span><br><span class="line">  if(!tokenStr)</span><br><span class="line">    return next(&#39;&#x2F;login&#39;);</span><br><span class="line"></span><br><span class="line">  next();</span><br><span class="line"></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">export default router </span><br></pre></td></tr></table></figure>

<p>实现退出功能<br>在Home组件中添加一个退出功能按钮,给退出按钮添加点击事件，添加事件处理代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">export default &#123;</span><br><span class="line">    methods:&#123;</span><br><span class="line">        logout()&#123;</span><br><span class="line">            window.sessionStorage.clear();</span><br><span class="line">            this.$router.push(&#39;&#x2F;login&#39;);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="5-5-处理ESLint警告"><a href="#5-5-处理ESLint警告" class="headerlink" title="5.5 处理ESLint警告"></a>5.5 处理ESLint警告</h2><p>打开脚手架面板，查看警告信息<br>[图片]<br>默认情况下，ESLint和vscode格式化工具有冲突，需要添加配置文件解决冲突。<br>在项目根目录添加 .prettierrc 文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    &quot;semi&quot;:false,</span><br><span class="line">    &quot;singleQuote&quot;:true</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>打开.eslintrc.js文件，禁用对 space-before-function-paren 的检查:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">rules: &#123;</span><br><span class="line">  &#39;no-console&#39;: process.env.NODE_ENV &#x3D;&#x3D;&#x3D; &#39;production&#39; ? &#39;error&#39; : &#39;off&#39;,</span><br><span class="line">  &#39;no-debugger&#39;: process.env.NODE_ENV &#x3D;&#x3D;&#x3D; &#39;production&#39; ? &#39;error&#39; : &#39;off&#39;,</span><br><span class="line">  &#39;space-before-function-paren&#39; : 0</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<h2 id="5-6-合并按需导入的element-ui"><a href="#5-6-合并按需导入的element-ui" class="headerlink" title="5.6 合并按需导入的element-ui"></a>5.6 合并按需导入的element-ui</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">import Vue from &#39;vue&#39;</span><br><span class="line">import &#123; Button, Form, FormItem, Input, Message &#125; from &#39;element-ui&#39;</span><br><span class="line"></span><br><span class="line">Vue.use(Button)</span><br><span class="line">Vue.use(Form)</span><br><span class="line">Vue.use(FormItem)</span><br><span class="line">Vue.use(Input)</span><br><span class="line">&#x2F;&#x2F; 进行全局挂载：</span><br><span class="line">Vue.prototype.$message &#x3D; Message</span><br></pre></td></tr></table></figure>

<h2 id="5-7-将代码提交到码云"><a href="#5-7-将代码提交到码云" class="headerlink" title="5.7 将代码提交到码云"></a>5.7 将代码提交到码云</h2><p>新建一个项目终端,输入命令‘git status’查看修改过的与新增的文件内容<br>将所有文件添加到暂存区：git add .<br>将所有代码提交到本地仓库：git commit -m “添加登录功能以及/home的基本结构”<br>查看分支： git branch  发现所有代码都被提交到了login分支<br>将login分支代码合并到master主分支，先切换到master：git checkout master<br>在master分支进行代码合并：git merge login<br>将本地的master推送到远端的码云：git push</p>
<p>(推送本地的子分支到码云，先切换到子分支：git checkout 分支名<br>然后推送到码云：git push -u origin 远端分支名)</p>
<h1 id="6-后台首页布局"><a href="#6-后台首页布局" class="headerlink" title="6.后台首页布局"></a>6.后台首页布局</h1><h2 id="6-1-git-操作"><a href="#6-1-git-操作" class="headerlink" title="6.1 git 操作"></a>6.1 git 操作</h2><p>创建一个分支进行开发，开发完毕之后将其合并到master<br>git checkout -b home<br>然后查看新创建的分支：git branch<br>确定我们正在使用home分支进行开发<br>然后推送到码云：git push -u origin home</p>
<h2 id="6-2-首页整体布局"><a href="#6-2-首页整体布局" class="headerlink" title="6.2 首页整体布局"></a>6.2 首页整体布局</h2><p>打开Home.vue组件，进行布局：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;el-container class&#x3D;&quot;home-container&quot;&gt;</span><br><span class="line">  &lt;!-- 头部区域 --&gt;</span><br><span class="line">  &lt;el-header&gt;Header&lt;el-button type&#x3D;&quot;info&quot; @click&#x3D;&quot;logout&quot;&gt; 退出 &lt;&#x2F;el-button&gt;&lt;&#x2F;el-header&gt;</span><br><span class="line">  &lt;!-- 页面主体区域 --&gt;</span><br><span class="line">  &lt;el-container&gt;</span><br><span class="line">    &lt;!-- 侧边栏 --&gt;</span><br><span class="line">    &lt;el-aside width&#x3D;&quot;200px&quot;&gt;Aside&lt;&#x2F;el-aside&gt;</span><br><span class="line">    &lt;!-- 主体结构 --&gt;</span><br><span class="line">    &lt;el-main&gt;Main&lt;&#x2F;el-main&gt;</span><br><span class="line">  &lt;&#x2F;el-container&gt;</span><br><span class="line">&lt;&#x2F;el-container&gt;</span><br></pre></td></tr></table></figure>

<p>默认情况下，跟element-ui组件同名的类名可以帮助我们快速的给对应的组件添加样式，如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">.home-container &#123;</span><br><span class="line">  height: 100%;</span><br><span class="line">&#125;</span><br><span class="line">.el-header&#123;</span><br><span class="line">  background-color:#373D41;</span><br><span class="line">&#125;</span><br><span class="line">.el-aside&#123;</span><br><span class="line">  background-color:#333744;</span><br><span class="line">&#125;</span><br><span class="line">.el-main&#123;</span><br><span class="line">  background-color:#eaedf1;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="6-3-顶部布局，侧边栏布局"><a href="#6-3-顶部布局，侧边栏布局" class="headerlink" title="6.3 顶部布局，侧边栏布局"></a>6.3 顶部布局，侧边栏布局</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;el-container class&#x3D;&quot;home-container&quot;&gt;</span><br><span class="line">      &lt;!-- 头部区域 --&gt;</span><br><span class="line">      &lt;el-header&gt;</span><br><span class="line">        &lt;div&gt;</span><br><span class="line">          &lt;!-- 黑马logo --&gt;</span><br><span class="line">          &lt;img src&#x3D;&quot;..&#x2F;assets&#x2F;heima.png&quot; alt&#x3D;&quot;&quot;&gt;</span><br><span class="line">          &lt;!-- 顶部标题 --&gt;</span><br><span class="line">          &lt;span&gt;电商后台管理系统&lt;&#x2F;span&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">        &lt;el-button type&#x3D;&quot;info&quot; @click&#x3D;&quot;logout&quot;&gt; 退出 &lt;&#x2F;el-button&gt;</span><br><span class="line">      &lt;&#x2F;el-header&gt;</span><br><span class="line">      &lt;!-- 页面主体区域 --&gt;</span><br><span class="line">      &lt;el-container&gt;</span><br><span class="line">        &lt;!-- 侧边栏 --&gt;</span><br><span class="line">        &lt;el-aside width&#x3D;&quot;200px&quot;&gt;</span><br><span class="line">          &lt;!-- 侧边栏菜单 --&gt;</span><br><span class="line">          &lt;el-menu</span><br><span class="line">            background-color&#x3D;&quot;#333744&quot;</span><br><span class="line">            text-color&#x3D;&quot;#fff&quot;</span><br><span class="line">            active-text-color&#x3D;&quot;#ffd04b&quot;&gt;</span><br><span class="line">            &lt;!-- 一级菜单 --&gt;</span><br><span class="line">            &lt;el-submenu index&#x3D;&quot;1&quot;&gt;</span><br><span class="line">              &lt;!-- 一级菜单模板 --&gt;</span><br><span class="line">              &lt;template slot&#x3D;&quot;title&quot;&gt;</span><br><span class="line">                &lt;!-- 图标 --&gt;</span><br><span class="line">                &lt;i class&#x3D;&quot;el-icon-location&quot;&gt;&lt;&#x2F;i&gt;</span><br><span class="line">                &lt;!-- 文本 --&gt;</span><br><span class="line">                &lt;span&gt;导航一&lt;&#x2F;span&gt;</span><br><span class="line">              &lt;&#x2F;template&gt;</span><br><span class="line">              &lt;!-- 二级子菜单 --&gt;</span><br><span class="line">              &lt;el-menu-item index&#x3D;&quot;1-4-1&quot;&gt;</span><br><span class="line">                &lt;!-- 二级菜单模板 --&gt;</span><br><span class="line">                &lt;template slot&#x3D;&quot;title&quot;&gt;</span><br><span class="line">                  &lt;!-- 图标 --&gt;</span><br><span class="line">                  &lt;i class&#x3D;&quot;el-icon-location&quot;&gt;&lt;&#x2F;i&gt;</span><br><span class="line">                  &lt;!-- 文本 --&gt;</span><br><span class="line">                  &lt;span&gt;子菜单一&lt;&#x2F;span&gt;</span><br><span class="line">                &lt;&#x2F;template&gt;</span><br><span class="line">              &lt;&#x2F;el-menu-item&gt;</span><br><span class="line">            &lt;&#x2F;el-submenu&gt;</span><br><span class="line">            </span><br><span class="line">          &lt;&#x2F;el-menu&gt;</span><br><span class="line">        &lt;&#x2F;el-aside&gt;</span><br><span class="line">        &lt;!-- 主体结构 --&gt;</span><br><span class="line">        &lt;el-main&gt;Main&lt;&#x2F;el-main&gt;</span><br><span class="line">      &lt;&#x2F;el-container&gt;</span><br><span class="line">    &lt;&#x2F;el-container&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br></pre></td></tr></table></figure>

<h2 id="6-4-axios请求拦截器"><a href="#6-4-axios请求拦截器" class="headerlink" title="6.4 axios请求拦截器"></a>6.4 axios请求拦截器</h2><p>后台除了登录接口之外，都需要token权限验证，我们可以通过添加axios请求拦截器来添加token，以保证拥有获取数据的权限<br>在main.js中添加代码，在将axios挂载到vue原型之前添加下面的代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 请求在到达服务器之前，先会调用use中的这个回调函数来添加请求头信息</span><br><span class="line">axios.interceptors.request.use(config&#x3D;&gt;&#123;</span><br><span class="line">  &#x2F;&#x2F; 为请求头对象，添加token验证的Authorization字段</span><br><span class="line">  config.headers.Authorization &#x3D; window.sessionStorage.getItem(&quot;token&quot;)</span><br><span class="line">  return config</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="6-5-请求侧边栏数据"><a href="#6-5-请求侧边栏数据" class="headerlink" title="6.5 请求侧边栏数据"></a>6.5 请求侧边栏数据</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      &#x2F;&#x2F; 左侧菜单数据</span><br><span class="line">      menuList: null</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  created() &#123;</span><br><span class="line">    &#x2F;&#x2F; 在created阶段请求左侧菜单数据</span><br><span class="line">    this.getMenuList()</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    logout() &#123;</span><br><span class="line">      window.sessionStorage.clear()</span><br><span class="line">      this.$router.push(&#39;&#x2F;login&#39;)</span><br><span class="line">    &#125;,</span><br><span class="line">    async getMenuList() &#123;</span><br><span class="line">      &#x2F;&#x2F; 发送请求获取左侧菜单数据</span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.get(&#39;menus&#39;)</span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200) return this.$message.error(res.meta.msg)</span><br><span class="line"></span><br><span class="line">      this.menuList &#x3D; res.data</span><br><span class="line">      console.log(res)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<p>通过v-for双重循环渲染左侧菜单</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&lt;el-menu</span><br><span class="line">  background-color&#x3D;&quot;#333744&quot;</span><br><span class="line">  text-color&#x3D;&quot;#fff&quot;</span><br><span class="line">  active-text-color&#x3D;&quot;#ffd04b&quot;&gt;</span><br><span class="line">  &lt;!-- 一级菜单 --&gt;</span><br><span class="line">  &lt;el-submenu :index&#x3D;&quot;item.id+&#39;&#39;&quot; v-for&#x3D;&quot;item in menuList&quot; :key&#x3D;&quot;item.id&quot;&gt;</span><br><span class="line">    &lt;!-- 一级菜单模板 --&gt;</span><br><span class="line">    &lt;template slot&#x3D;&quot;title&quot;&gt;</span><br><span class="line">      &lt;!-- 图标 --&gt;</span><br><span class="line">      &lt;i class&#x3D;&quot;el-icon-location&quot;&gt;&lt;&#x2F;i&gt;</span><br><span class="line">      &lt;!-- 文本 --&gt;</span><br><span class="line">      &lt;span&gt;&#123;&#123;item.authName&#125;&#125;&lt;&#x2F;span&gt;</span><br><span class="line">    &lt;&#x2F;template&gt;</span><br><span class="line">    &lt;!-- 二级子菜单 --&gt;</span><br><span class="line">    &lt;el-menu-item :index&#x3D;&quot;subItem.id+&#39;&#39;&quot; v-for&#x3D;&quot;subItem in item.children&quot; :key&#x3D;&quot;subItem.id&quot;&gt;</span><br><span class="line">      &lt;!-- 二级菜单模板 --&gt;</span><br><span class="line">      &lt;template slot&#x3D;&quot;title&quot;&gt;</span><br><span class="line">        &lt;!-- 图标 --&gt;</span><br><span class="line">        &lt;i class&#x3D;&quot;el-icon-location&quot;&gt;&lt;&#x2F;i&gt;</span><br><span class="line">        &lt;!-- 文本 --&gt;</span><br><span class="line">        &lt;span&gt;&#123;&#123;subItem.authName&#125;&#125;&lt;&#x2F;span&gt;</span><br><span class="line">      &lt;&#x2F;template&gt;</span><br><span class="line">    &lt;&#x2F;el-menu-item&gt;</span><br><span class="line">  &lt;&#x2F;el-submenu&gt;</span><br><span class="line">&lt;&#x2F;el-menu&gt;</span><br></pre></td></tr></table></figure>

<h2 id="6-6-设置激活子菜单样式"><a href="#6-6-设置激活子菜单样式" class="headerlink" title="6.6 设置激活子菜单样式"></a>6.6 设置激活子菜单样式</h2><p>通过更改el-menu的active-text-color属性可以设置侧边栏菜单中点击的激活项的文字颜色<br>通过更改菜单项模板（template）中的i标签的类名，可以将左侧菜单栏的图标进行设置，我们需要在项目中使用第三方字体图标<br>在数据中添加一个iconsObj：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">iconsObj: &#123;</span><br><span class="line">    <span class="string">&#x27;125&#x27;</span>:<span class="string">&#x27;iconfont icon-user&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;103&#x27;</span>:<span class="string">&#x27;iconfont icon-tijikongjian&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;101&#x27;</span>:<span class="string">&#x27;iconfont icon-shangpin&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;102&#x27;</span>:<span class="string">&#x27;iconfont icon-danju&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;145&#x27;</span>:<span class="string">&#x27;iconfont icon-baobiao&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后将图标类名进行数据绑定，绑定iconsObj中的数据：</p>
<p>为了保持左侧菜单每次只能打开一个，显示其中的子菜单，我们可以在el-menu中添加一个属性unique-opened<br>或者也可以数据绑定进行设置(此时true认为是一个bool值，而不是字符串) :unique-opened=”true”</p>
<h2 id="6-7-制作侧边菜单栏的伸缩功能"><a href="#6-7-制作侧边菜单栏的伸缩功能" class="headerlink" title="6.7 制作侧边菜单栏的伸缩功能"></a>6.7 制作侧边菜单栏的伸缩功能</h2><p>在菜单栏上方添加一个div</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 侧边栏,宽度根据是否折叠进行设置 --&gt;</span><br><span class="line">&lt;el-aside :width&#x3D;&quot;isCollapse ? &#39;64px&#39;:&#39;200px&#39;&quot;&gt;</span><br><span class="line">  &lt;!-- 伸缩侧边栏按钮 --&gt;</span><br><span class="line">  &lt;div class&#x3D;&quot;toggle-button&quot; @click&#x3D;&quot;toggleCollapse&quot;&gt;|||&lt;&#x2F;div&gt;</span><br><span class="line">  &lt;!-- 侧边栏菜单，:collapse&#x3D;&quot;isCollapse&quot;（设置折叠菜单为绑定的 isCollapse 值），:collapse-transition&#x3D;&quot;false&quot;（关闭默认的折叠动画） --&gt;</span><br><span class="line">  &lt;el-menu</span><br><span class="line">  :collapse&#x3D;&quot;isCollapse&quot;</span><br><span class="line">  :collapse-transition&#x3D;&quot;false&quot;</span><br><span class="line">  ......</span><br></pre></td></tr></table></figure>

<p>然后给div添加样式，给div添加事件，实现伸缩：</p>
<div class="toggle-button" @click="this.isCollapse ? '64px':'200px'">|||</div>

<h2 id="6-8-在后台首页添加子级路由"><a href="#6-8-在后台首页添加子级路由" class="headerlink" title="6.8 在后台首页添加子级路由"></a>6.8 在后台首页添加子级路由</h2><p>新增子级路由组件Welcome.vue<br>在router.js中导入子级路由组件，并设置路由规则以及子级路由的默认重定向<br>打开Home.vue，在main的主体结构中添加一个路由占位符</p>
<p>制作好了Welcome子级路由之后，我们需要将所有的侧边栏二级菜单都改造成子级路由链接<br>我们只需要将el-menu的router属性设置为true就可以了，此时当我们点击二级菜单的时候，就会根据菜单的index<br>属性进行路由跳转,如: /110,<br>使用index id来作为跳转的路径不合适，我们可以重新绑定index的值为  :index=”‘/‘+subItem.path”</p>
<h1 id="7-完成用户列表开发"><a href="#7-完成用户列表开发" class="headerlink" title="7.完成用户列表开发"></a>7.完成用户列表开发</h1><h2 id="7-1-完成用户列表主体区域"><a href="#7-1-完成用户列表主体区域" class="headerlink" title="7.1 完成用户列表主体区域"></a>7.1 完成用户列表主体区域</h2><p>新建用户列表组件  user/Users.vue<br>在router.js中导入子级路由组件Users.vue，并设置路由规则</p>
<p>当点击二级菜单的时候，被点击的二级子菜单并没有高亮，我们需要正在被使用的功能高亮显示<br>我们可以通过设置el-menu的default-active属性来设置当前激活菜单的index<br>但是default-active属性也不能写死，固定为某个菜单值<br>所以我们可以先给所有的二级菜单添加点击事件,并将path值作为方法的参数<br>@click=”saveNavState(‘/‘+subItem.path)”<br>在saveNavState方法中将path保存到sessionStorage中<br>saveNavState( path ){<br>  //点击二级菜单的时候保存被点击的二级菜单信息<br>  window.sessionStorage.setItem(“activePath”,path);<br>  this.activePath = path;<br>}<br>然后在数据中添加一个activePath绑定数据，并将el-menu的default-active属性设置为activePath<br>最后在created中将sessionStorage中的数据赋值给activePath<br>this.activePath = window.sessionStorage.getItem(“activePath”)</p>
<h2 id="7-2-绘制用户列表基本结构"><a href="#7-2-绘制用户列表基本结构" class="headerlink" title="7.2 绘制用户列表基本结构"></a>7.2 绘制用户列表基本结构</h2><h3 id="7-2-1-使用element-ui面包屑组件完成顶部导航路径"><a href="#7-2-1-使用element-ui面包屑组件完成顶部导航路径" class="headerlink" title="7.2.1 使用element-ui面包屑组件完成顶部导航路径"></a>7.2.1 使用element-ui面包屑组件完成顶部导航路径</h3><p>复制面包屑代码，在element.js中导入组件Breadcrumb,BreadcrumbItem</p>
<h3 id="7-2-2-使用element-ui卡片组件完成主体表格"><a href="#7-2-2-使用element-ui卡片组件完成主体表格" class="headerlink" title="7.2.2 使用element-ui卡片组件完成主体表格"></a>7.2.2 使用element-ui卡片组件完成主体表格</h3><p>复制卡片组件代码，在element.js中导入组件Card，再使用element-ui输入框完成搜索框及搜索按钮，<br>此时我们需要使用栅格布局来划分结构(复制卡片组件代码，在element.js中导入组件Row，Col)，然后再使用el-button制作添加用户按钮</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&lt;div&gt;</span><br><span class="line">    &lt;h3&gt;用户列表组件&lt;&#x2F;h3&gt;</span><br><span class="line">    &lt;!-- 面包屑导航 --&gt;</span><br><span class="line">    &lt;el-breadcrumb separator&#x3D;&quot;&#x2F;&quot;&gt;</span><br><span class="line">        &lt;el-breadcrumb-item :to&#x3D;&quot;&#123; path: &#39;&#x2F;home&#39; &#125;&quot;&gt;首页&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">        &lt;el-breadcrumb-item&gt;用户管理&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">        &lt;el-breadcrumb-item&gt;用户列表&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">    &lt;&#x2F;el-breadcrumb&gt;</span><br><span class="line">    &lt;!-- 卡片视图区域 --&gt;</span><br><span class="line">    &lt;el-card&gt;</span><br><span class="line">        &lt;!-- 搜索与添加区域 --&gt;</span><br><span class="line">        &lt;el-row :gutter&#x3D;&quot;20&quot;&gt;</span><br><span class="line">            &lt;el-col :span&#x3D;&quot;7&quot;&gt;</span><br><span class="line">                &lt;el-input placeholder&#x3D;&quot;请输入内容&quot;&gt;</span><br><span class="line">                    &lt;el-button slot&#x3D;&quot;append&quot; icon&#x3D;&quot;el-icon-search&quot;&gt;&lt;&#x2F;el-button&gt;</span><br><span class="line">                &lt;&#x2F;el-input&gt; </span><br><span class="line">            &lt;&#x2F;el-col&gt;</span><br><span class="line">            &lt;el-col :span&#x3D;&quot;4&quot;&gt;</span><br><span class="line">                &lt;el-button type&#x3D;&quot;primary&quot;&gt;添加用户&lt;&#x2F;el-button&gt;</span><br><span class="line">            &lt;&#x2F;el-col&gt;</span><br><span class="line">        &lt;&#x2F;el-row&gt; </span><br><span class="line">    &lt;&#x2F;el-card&gt;</span><br><span class="line">&lt;&#x2F;div&gt;</span><br></pre></td></tr></table></figure>

<h2 id="7-3-请求用户列表数据"><a href="#7-3-请求用户列表数据" class="headerlink" title="7.3 请求用户列表数据"></a>7.3 请求用户列表数据</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      &#x2F;&#x2F;获取查询用户信息的参数</span><br><span class="line">      queryInfo: &#123;</span><br><span class="line">        query: &#39;&#39;,</span><br><span class="line">        pagenum: 1,</span><br><span class="line">        pagesize: 2</span><br><span class="line">      &#125;,</span><br><span class="line">      &#x2F;&#x2F;保存请求回来的用户列表数据</span><br><span class="line">      userList:[],</span><br><span class="line">      total:0</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  created() &#123;</span><br><span class="line">    this.getUserList()</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    async getUserList() &#123;</span><br><span class="line">      &#x2F;&#x2F;发送请求获取用户列表数据</span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.get(&#39;users&#39;, &#123;</span><br><span class="line">        params: this.queryInfo</span><br><span class="line">      &#125;)</span><br><span class="line">      &#x2F;&#x2F;如果返回状态为异常状态则报错并返回</span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200)</span><br><span class="line">        return this.$message.error(&#39;获取用户列表失败&#39;)</span><br><span class="line">      &#x2F;&#x2F;如果返回状态正常，将请求的数据保存在data中</span><br><span class="line">      this.userList &#x3D; res.data.users;</span><br><span class="line">      this.total &#x3D; res.data.total;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<h2 id="7-4-将用户列表数据展示"><a href="#7-4-将用户列表数据展示" class="headerlink" title="7.4 将用户列表数据展示"></a>7.4 将用户列表数据展示</h2><p>使用表格来展示用户列表数据，使用element-ui表格组件完成列表展示数据(复制表格代码，在element.js中导入组件Table,TableColumn)<br>在渲染展示状态时，会使用作用域插槽获取每一行的数据<br>再使用switch开关组件展示状态信息(复制开关组件代码，在element.js中导入组件Switch)<br>而渲染操作列时，也是使用作用域插槽来进行渲染的，<br>在操作列中包含了修改，删除，分配角色按钮，当我们把鼠标放到分配角色按钮上时<br>希望能有一些文字提示，此时我们需要使用文字提示组件(复制文字提示组件代码，在element.js中导入组件Tooltip),将分配角色按钮包含<br>代码结构如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 用户列表(表格)区域 --&gt;</span><br><span class="line">&lt;el-table :data&#x3D;&quot;userList&quot; border stripe&gt;</span><br><span class="line">    &lt;el-table-column type&#x3D;&quot;index&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">    &lt;el-table-column label&#x3D;&quot;姓名&quot; prop&#x3D;&quot;username&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">    &lt;el-table-column label&#x3D;&quot;邮箱&quot; prop&#x3D;&quot;email&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">    &lt;el-table-column label&#x3D;&quot;电话&quot; prop&#x3D;&quot;mobile&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">    &lt;el-table-column label&#x3D;&quot;角色&quot; prop&#x3D;&quot;role_name&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">    &lt;el-table-column label&#x3D;&quot;状态&quot;&gt;</span><br><span class="line">        &lt;template slot-scope&#x3D;&quot;scope&quot;&gt;</span><br><span class="line">            &lt;el-switch v-model&#x3D;&quot;scope.row.mg_state&quot;&gt;&lt;&#x2F;el-switch&gt;</span><br><span class="line">        &lt;&#x2F;template&gt;</span><br><span class="line">    &lt;&#x2F;el-table-column&gt;</span><br><span class="line">    &lt;el-table-column label&#x3D;&quot;操作&quot; width&#x3D;&quot;180px&quot;&gt;</span><br><span class="line">        &lt;template slot-scope&#x3D;&quot;scope&quot;&gt;</span><br><span class="line">            &lt;!-- 修改 --&gt;</span><br><span class="line">            &lt;el-button type&#x3D;&quot;primary&quot; icon&#x3D;&quot;el-icon-edit&quot; size&#x3D;&#39;mini&#39;&gt;&lt;&#x2F;el-button&gt;</span><br><span class="line">            &lt;!-- 删除 --&gt;</span><br><span class="line">            &lt;el-button type&#x3D;&quot;danger&quot; icon&#x3D;&quot;el-icon-delete&quot; size&#x3D;&#39;mini&#39;&gt;&lt;&#x2F;el-button&gt;</span><br><span class="line">            &lt;!-- 分配角色 --&gt;</span><br><span class="line">            &lt;el-tooltip class&#x3D;&quot;item&quot; effect&#x3D;&quot;dark&quot; content&#x3D;&quot;分配角色&quot; placement&#x3D;&quot;top&quot; :enterable&#x3D;&quot;false&quot;&gt;</span><br><span class="line">                &lt;el-button type&#x3D;&quot;warning&quot; icon&#x3D;&quot;el-icon-setting&quot; size&#x3D;&#39;mini&#39;&gt;&lt;&#x2F;el-button&gt;</span><br><span class="line">            &lt;&#x2F;el-tooltip&gt;</span><br><span class="line">        &lt;&#x2F;template&gt;</span><br><span class="line">    &lt;&#x2F;el-table-column&gt;</span><br><span class="line">&lt;&#x2F;el-table&gt;</span><br></pre></td></tr></table></figure>

<h2 id="7-5-实现用户列表分页"><a href="#7-5-实现用户列表分页" class="headerlink" title="7.5 实现用户列表分页"></a>7.5 实现用户列表分页</h2><h3 id="7-5-1使用表格来展示用户列表数据"><a href="#7-5-1使用表格来展示用户列表数据" class="headerlink" title="7.5.1使用表格来展示用户列表数据"></a>7.5.1使用表格来展示用户列表数据</h3><p>可以使用分页组件完成列表分页展示数据(复制分页组件代码，在element.js中导入组件Pagination)</p>
<h3 id="7-5-2-更改组件中的绑定数据"><a href="#7-5-2-更改组件中的绑定数据" class="headerlink" title="7.5.2 更改组件中的绑定数据"></a>7.5.2 更改组件中的绑定数据</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 分页导航区域 </span><br><span class="line">@size-change(pagesize改变时触发) </span><br><span class="line">@current-change(页码发生改变时触发)</span><br><span class="line">:current-page(设置当前页码)</span><br><span class="line">:page-size(设置每页的数据条数)</span><br><span class="line">:total(设置总页数) --&gt;</span><br><span class="line">            &lt;el-pagination @size-change&#x3D;&quot;handleSizeChange&quot; @current-change&#x3D;&quot;handleCurrentChange&quot; :current-page&#x3D;&quot;queryInfo.pagenum&quot; :page-sizes&#x3D;&quot;[1, 2, 5, 10]&quot; :page-size&#x3D;&quot;queryInfo.pagesize&quot; layout&#x3D;&quot;total, sizes, prev, pager, next, jumper&quot; :total&#x3D;&quot;total&quot;&gt;</span><br><span class="line">            &lt;&#x2F;el-pagination&gt;</span><br></pre></td></tr></table></figure>

<h3 id="7-5-3-添加两个事件的事件处理函数-size-change，-current-change"><a href="#7-5-3-添加两个事件的事件处理函数-size-change，-current-change" class="headerlink" title="7.5.3 添加两个事件的事件处理函数@size-change，@current-change"></a>7.5.3 添加两个事件的事件处理函数@size-change，@current-change</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">handleSizeChange(newSize) &#123;</span><br><span class="line">  &#x2F;&#x2F;pagesize改变时触发，当pagesize发生改变的时候，我们应该</span><br><span class="line">  &#x2F;&#x2F;以最新的pagesize来请求数据并展示数据</span><br><span class="line">  &#x2F;&#x2F;   console.log(newSize)</span><br><span class="line">  this.queryInfo.pagesize &#x3D; newSize;</span><br><span class="line">  &#x2F;&#x2F;重新按照pagesize发送请求，请求最新的数据</span><br><span class="line">  this.getUserList();  </span><br><span class="line">&#125;,</span><br><span class="line">handleCurrentChange( current ) &#123;</span><br><span class="line">  &#x2F;&#x2F;页码发生改变时触发当current发生改变的时候，我们应该</span><br><span class="line">  &#x2F;&#x2F;以最新的current页码来请求数据并展示数据</span><br><span class="line">  &#x2F;&#x2F;   console.log(current)</span><br><span class="line">  this.queryInfo.pagenum &#x3D; current;</span><br><span class="line">  &#x2F;&#x2F;重新按照pagenum发送请求，请求最新的数据</span><br><span class="line">  this.getUserList();  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="7-6-实现更新用户状态"><a href="#7-6-实现更新用户状态" class="headerlink" title="7.6 实现更新用户状态"></a>7.6 实现更新用户状态</h2><p>当用户点击列表中的switch组件时，用户的状态应该跟随发生改变。</p>
<h3 id="7-6-1-首先监听用户点击switch组件的事件，并将作用域插槽的数据当做事件参数进行传递"><a href="#7-6-1-首先监听用户点击switch组件的事件，并将作用域插槽的数据当做事件参数进行传递" class="headerlink" title="7.6.1 首先监听用户点击switch组件的事件，并将作用域插槽的数据当做事件参数进行传递"></a>7.6.1 首先监听用户点击switch组件的事件，并将作用域插槽的数据当做事件参数进行传递</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;el-switch v-model&#x3D;&quot;scope.row.mg_state&quot; @change&#x3D;&quot;userStateChanged(scope.row)&quot;&gt;&lt;&#x2F;el-switch&gt;</span><br></pre></td></tr></table></figure>

<h3 id="7-6-2-在事件中发送请求完成状态的更改"><a href="#7-6-2-在事件中发送请求完成状态的更改" class="headerlink" title="7.6.2 在事件中发送请求完成状态的更改"></a>7.6.2 在事件中发送请求完成状态的更改</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">async userStateChanged(row) &#123;</span><br><span class="line">  &#x2F;&#x2F;发送请求进行状态修改</span><br><span class="line">  const &#123; data: res &#125; &#x3D; await this.$http.put(</span><br><span class="line">    &#96;users&#x2F;$&#123;row.id&#125;&#x2F;state&#x2F;$&#123;row.mg_state&#125;&#96;</span><br><span class="line">  )</span><br><span class="line">  &#x2F;&#x2F;如果返回状态为异常状态则报错并返回</span><br><span class="line">  if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">    row.mg_state &#x3D; !row.mg_state</span><br><span class="line">    return this.$message.error(&#39;修改状态失败&#39;)</span><br><span class="line">  &#125;</span><br><span class="line">  this.$message.success(&#39;更新状态成功&#39;)</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<h2 id="7-7-实现搜索功能"><a href="#7-7-实现搜索功能" class="headerlink" title="7.7 实现搜索功能"></a>7.7 实现搜索功能</h2><p>添加数据绑定，添加搜索按钮的点击事件(当用户点击搜索按钮的时候，调用getUserList方法根据文本框内容重新请求用户列表数据)<br>当我们在输入框中输入内容并点击搜索之后，会按照搜索关键字搜索，我们希望能够提供一个X删除搜索关键字并重新获取所有的用户列表数据，只需要给文本框添加clearable属性并添加clear事件，在clear事件中重新请求数据即可</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;el-col :span&#x3D;&quot;7&quot;&gt;</span><br><span class="line">    &lt;el-input placeholder&#x3D;&quot;请输入内容&quot; v-model&#x3D;&quot;queryInfo.query&quot; clearable @clear&#x3D;&quot;getUserList&quot;&gt;</span><br><span class="line">        &lt;el-button slot&#x3D;&quot;append&quot; icon&#x3D;&quot;el-icon-search&quot; @click&#x3D;&quot;getUserList&quot;&gt;&lt;&#x2F;el-button&gt;</span><br><span class="line">    &lt;&#x2F;el-input&gt;</span><br><span class="line">&lt;&#x2F;el-col&gt;</span><br></pre></td></tr></table></figure>

<h2 id="7-8-实现添加用户"><a href="#7-8-实现添加用户" class="headerlink" title="7.8 实现添加用户"></a>7.8 实现添加用户</h2><h3 id="7-8-1-当我们点击添加用户按钮的时候，弹出一个对话框来实现添加用户的功能，首先我们需要复制对话框组件的代码并在element-js文件中引入Dialog组件"><a href="#7-8-1-当我们点击添加用户按钮的时候，弹出一个对话框来实现添加用户的功能，首先我们需要复制对话框组件的代码并在element-js文件中引入Dialog组件" class="headerlink" title="7.8.1 当我们点击添加用户按钮的时候，弹出一个对话框来实现添加用户的功能，首先我们需要复制对话框组件的代码并在element.js文件中引入Dialog组件"></a>7.8.1 当我们点击添加用户按钮的时候，弹出一个对话框来实现添加用户的功能，首先我们需要复制对话框组件的代码并在element.js文件中引入Dialog组件</h3><h3 id="7-8-2-接下来我们要为“添加用户”按钮添加点击事件，在事件中将addDialogVisible设置为true，即显示对话框"><a href="#7-8-2-接下来我们要为“添加用户”按钮添加点击事件，在事件中将addDialogVisible设置为true，即显示对话框" class="headerlink" title="7.8.2 接下来我们要为“添加用户”按钮添加点击事件，在事件中将addDialogVisible设置为true，即显示对话框"></a>7.8.2 接下来我们要为“添加用户”按钮添加点击事件，在事件中将addDialogVisible设置为true，即显示对话框</h3><h3 id="7-8-3-更改Dialog组件中的内容"><a href="#7-8-3-更改Dialog组件中的内容" class="headerlink" title="7.8.3 更改Dialog组件中的内容"></a>7.8.3 更改Dialog组件中的内容</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 对话框组件  :visible.sync(设置是否显示对话框) width(设置对话框的宽度)</span><br><span class="line">:before-close(在对话框关闭前触发的事件) --&gt;</span><br><span class="line">&lt;el-dialog title&#x3D;&quot;添加用户&quot; :visible.sync&#x3D;&quot;addDialogVisible&quot; width&#x3D;&quot;50%&quot;&gt;</span><br><span class="line">    &lt;!-- 对话框主体区域 --&gt;</span><br><span class="line">    &lt;el-form :model&#x3D;&quot;addForm&quot; :rules&#x3D;&quot;addFormRules&quot; ref&#x3D;&quot;addFormRef&quot; label-width&#x3D;&quot;70px&quot;&gt;</span><br><span class="line">        &lt;el-form-item label&#x3D;&quot;用户名&quot; prop&#x3D;&quot;username&quot;&gt;</span><br><span class="line">            &lt;el-input v-model&#x3D;&quot;addForm.username&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">        &lt;&#x2F;el-form-item&gt;</span><br><span class="line">        &lt;el-form-item label&#x3D;&quot;密码&quot; prop&#x3D;&quot;password&quot;&gt;</span><br><span class="line">            &lt;el-input v-model&#x3D;&quot;addForm.password&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">        &lt;&#x2F;el-form-item&gt;</span><br><span class="line">        &lt;el-form-item label&#x3D;&quot;邮箱&quot; prop&#x3D;&quot;email&quot;&gt;</span><br><span class="line">            &lt;el-input v-model&#x3D;&quot;addForm.email&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">        &lt;&#x2F;el-form-item&gt;</span><br><span class="line">        &lt;el-form-item label&#x3D;&quot;电话&quot; prop&#x3D;&quot;mobile&quot;&gt;</span><br><span class="line">            &lt;el-input v-model&#x3D;&quot;addForm.mobile&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">        &lt;&#x2F;el-form-item&gt;</span><br><span class="line">    &lt;&#x2F;el-form&gt;</span><br><span class="line">    &lt;!-- 对话框底部区域 --&gt;</span><br><span class="line">    &lt;span slot&#x3D;&quot;footer&quot; class&#x3D;&quot;dialog-footer&quot;&gt;</span><br><span class="line">        &lt;el-button @click&#x3D;&quot;addDialogVisible &#x3D; false&quot;&gt;取 消&lt;&#x2F;el-button&gt;</span><br><span class="line">        &lt;el-button type&#x3D;&quot;primary&quot; @click&#x3D;&quot;addDialogVisible &#x3D; false&quot;&gt;确 定&lt;&#x2F;el-button&gt;</span><br><span class="line">    &lt;&#x2F;span&gt;</span><br><span class="line">&lt;&#x2F;el-dialog&gt;</span><br></pre></td></tr></table></figure>

<h3 id="7-8-4-添加数据绑定和校验规则："><a href="#7-8-4-添加数据绑定和校验规则：" class="headerlink" title="7.8.4 添加数据绑定和校验规则："></a>7.8.4 添加数据绑定和校验规则：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line">data() &#123;</span><br><span class="line">  &#x2F;&#x2F;验证邮箱的规则</span><br><span class="line">  var checkEmail &#x3D; (rule, value, cb) &#x3D;&gt; &#123;</span><br><span class="line">    const regEmail &#x3D; &#x2F;^\w+@\w+(\.\w+)+$&#x2F;</span><br><span class="line">    if (regEmail.test(value)) &#123;</span><br><span class="line">      return cb()</span><br><span class="line">    &#125;</span><br><span class="line">    &#x2F;&#x2F;返回一个错误提示</span><br><span class="line">    cb(new Error(&#39;请输入合法的邮箱&#39;))</span><br><span class="line">  &#125;</span><br><span class="line">  &#x2F;&#x2F;验证手机号码的规则</span><br><span class="line">  var checkMobile &#x3D; (rule, value, cb) &#x3D;&gt; &#123;</span><br><span class="line">    const regMobile &#x3D; &#x2F;^1[34578]\d&#123;9&#125;$&#x2F;</span><br><span class="line">    if (regMobile.test(value)) &#123;</span><br><span class="line">      return cb()</span><br><span class="line">    &#125;</span><br><span class="line">    &#x2F;&#x2F;返回一个错误提示</span><br><span class="line">    cb(new Error(&#39;请输入合法的手机号码&#39;))</span><br><span class="line">  &#125;</span><br><span class="line">  return &#123;</span><br><span class="line">    &#x2F;&#x2F;获取查询用户信息的参数</span><br><span class="line">    queryInfo: &#123;</span><br><span class="line">      &#x2F;&#x2F; 查询的条件</span><br><span class="line">      query: &#39;&#39;,</span><br><span class="line">      &#x2F;&#x2F; 当前的页数，即页码</span><br><span class="line">      pagenum: 1,</span><br><span class="line">      &#x2F;&#x2F; 每页显示的数据条数</span><br><span class="line">      pagesize: 2</span><br><span class="line">    &#125;,</span><br><span class="line">    &#x2F;&#x2F;保存请求回来的用户列表数据</span><br><span class="line">    userList: [],</span><br><span class="line">    total: 0,</span><br><span class="line">    &#x2F;&#x2F;是否显示添加用户弹出窗</span><br><span class="line">    addDialogVisible: false,</span><br><span class="line">    &#x2F;&#x2F; 添加用户的表单数据</span><br><span class="line">    addForm: &#123;</span><br><span class="line">      username: &#39;&#39;,</span><br><span class="line">      password: &#39;&#39;,</span><br><span class="line">      email: &#39;&#39;,</span><br><span class="line">      mobile: &#39;&#39;</span><br><span class="line">    &#125;,</span><br><span class="line">    &#x2F;&#x2F; 添加表单的验证规则对象</span><br><span class="line">    addFormRules: &#123;</span><br><span class="line">      username: [</span><br><span class="line">        &#123; required: true, message: &#39;请输入用户名称&#39;, trigger: &#39;blur&#39; &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">          min: 3,</span><br><span class="line">          max: 10,</span><br><span class="line">          message: &#39;用户名在3~10个字符之间&#39;,</span><br><span class="line">          trigger: &#39;blur&#39;</span><br><span class="line">        &#125;</span><br><span class="line">      ],</span><br><span class="line">      password: [</span><br><span class="line">        &#123; required: true, message: &#39;请输入密码&#39;, trigger: &#39;blur&#39; &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">          min: 6,</span><br><span class="line">          max: 15,</span><br><span class="line">          message: &#39;用户名在6~15个字符之间&#39;,</span><br><span class="line">          trigger: &#39;blur&#39;</span><br><span class="line">        &#125;</span><br><span class="line">      ],</span><br><span class="line">      email: [</span><br><span class="line">          &#123; required: true, message: &#39;请输入邮箱&#39;, trigger: &#39;blur&#39; &#125;,</span><br><span class="line">          &#123; validator:checkEmail, message: &#39;邮箱格式不正确，请重新输入&#39;, trigger: &#39;blur&#39;&#125;</span><br><span class="line">      ],</span><br><span class="line">      mobile: [</span><br><span class="line">          &#123; required: true, message: &#39;请输入手机号码&#39;, trigger: &#39;blur&#39; &#125;,</span><br><span class="line">          &#123; validator:checkMobile, message: &#39;手机号码不正确，请重新输入&#39;, trigger: &#39;blur&#39;&#125;</span><br><span class="line">      ]</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="7-8-5当关闭对话框时，重置表单"><a href="#7-8-5当关闭对话框时，重置表单" class="headerlink" title="7.8.5当关闭对话框时，重置表单"></a>7.8.5当关闭对话框时，重置表单</h3><p>给el-dialog添加@close事件，在事件中添加重置表单的代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">methods:&#123;</span><br><span class="line">  ....</span><br><span class="line">  addDialogClosed()&#123;</span><br><span class="line">      &#x2F;&#x2F;对话框关闭之后，重置表达</span><br><span class="line">      this.$refs.addFormRef.resetFields();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="7-8-6-点击对话框中的确定按钮，发送请求完成添加用户的操作"><a href="#7-8-6-点击对话框中的确定按钮，发送请求完成添加用户的操作" class="headerlink" title="7.8.6 点击对话框中的确定按钮，发送请求完成添加用户的操作"></a>7.8.6 点击对话框中的确定按钮，发送请求完成添加用户的操作</h3><p>首先给确定按钮添加点击事件，在点击事件中完成业务逻辑代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">methods:&#123;</span><br><span class="line">  ....</span><br><span class="line">  addUser()&#123;</span><br><span class="line">      &#x2F;&#x2F;点击确定按钮，添加新用户</span><br><span class="line">      &#x2F;&#x2F;调用validate进行表单验证</span><br><span class="line">      this.$refs.addFormRef.validate( async valid &#x3D;&gt; &#123;</span><br><span class="line">          if(!valid) return this.$message.error(&quot;请填写完整用户信息&quot;);</span><br><span class="line">          &#x2F;&#x2F;发送请求完成添加用户的操作</span><br><span class="line">          const &#123;data:res&#125; &#x3D; await this.$http.post(&quot;users&quot;,this.addForm)</span><br><span class="line">          &#x2F;&#x2F;判断如果添加失败，就做提示</span><br><span class="line">          if (res.meta.status !&#x3D;&#x3D; 200)</span><br><span class="line">              return this.$message.error(&#39;添加用户失败&#39;)</span><br><span class="line">          &#x2F;&#x2F;添加成功的提示</span><br><span class="line">          this.$message.success(&quot;添加用户成功&quot;)</span><br><span class="line">          &#x2F;&#x2F;关闭对话框</span><br><span class="line">          this.addDialogVisible &#x3D; false</span><br><span class="line">          &#x2F;&#x2F;重新请求最新的数据</span><br><span class="line">          this.getUserList()</span><br><span class="line">      &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="7-9-推送代码"><a href="#7-9-推送代码" class="headerlink" title="7.9 推送代码"></a>7.9 推送代码</h2><p>新建一个项目终端,输入命令‘git status’查看修改过的与新增的文件内容<br>将所有文件添加到暂存区：git add .<br>将所有代码提交到本地仓库：git commit -m “添加用户列表页”<br>查看分支： git branch  发现所有代码都被提交到了home分支<br>将login分支代码合并到master主分支，先切换到master：git checkout master<br>在master分支进行代码合并：git merge home<br>将本地的master推送到远端的码云：git push</p>
<p>(推送本地的子分支到码云，先切换到子分支：git checkout 分支名<br>然后推送到码云：git push -u origin 远端分支名)</p>
<p>创建user子分支，并将代码推送到码云</p>
<h1 id="8-完成用户信息的修改与删除"><a href="#8-完成用户信息的修改与删除" class="headerlink" title="8.完成用户信息的修改与删除"></a>8.完成用户信息的修改与删除</h1><h2 id="8-1-git操作"><a href="#8-1-git操作" class="headerlink" title="8.1 git操作"></a>8.1 git操作</h2><p>创建user子分支：git checkout -b user</p>
<p> 将本地的user分支推送到码云：git push -u origin user</p>
<h2 id="8-2-修改用户信息"><a href="#8-2-修改用户信息" class="headerlink" title="8.2 修改用户信息"></a>8.2 修改用户信息</h2><h3 id="8-2-1-为用户列表中的修改按钮绑定点击事件"><a href="#8-2-1-为用户列表中的修改按钮绑定点击事件" class="headerlink" title="8.2.1 为用户列表中的修改按钮绑定点击事件"></a>8.2.1 为用户列表中的修改按钮绑定点击事件</h3><h3 id="8-2-2-在页面中添加修改用户对话框，并修改对话框的属性"><a href="#8-2-2-在页面中添加修改用户对话框，并修改对话框的属性" class="headerlink" title="8.2.2 在页面中添加修改用户对话框，并修改对话框的属性"></a>8.2.2 在页面中添加修改用户对话框，并修改对话框的属性</h3><h3 id="8-2-3-根据id查询需要修改的用户数据"><a href="#8-2-3-根据id查询需要修改的用户数据" class="headerlink" title="8.2.3 根据id查询需要修改的用户数据"></a>8.2.3 根据id查询需要修改的用户数据</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;展示编辑用户的对话框</span><br><span class="line">async showEditDialog(id) &#123;</span><br><span class="line">    &#x2F;&#x2F;发送请求根据id获取用户信息</span><br><span class="line">    const &#123; data: res &#125; &#x3D; await this.$http.get(&#39;users&#x2F;&#39; + id)</span><br><span class="line">    &#x2F;&#x2F;判断如果添加失败，就做提示</span><br><span class="line">    if (res.meta.status !&#x3D;&#x3D; 200) return this.$message.error(&#39;获取用户信息失败&#39;)</span><br><span class="line">    &#x2F;&#x2F;将获取到的数据保存到数据editForm中</span><br><span class="line">    this.editForm &#x3D; res.data</span><br><span class="line">    &#x2F;&#x2F; 显示弹出窗</span><br><span class="line">    this.editDialogVisible &#x3D; true</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="8-2-4-在弹出窗中添加修改用户信息的表单并做响应的数据绑定以及数据验证"><a href="#8-2-4-在弹出窗中添加修改用户信息的表单并做响应的数据绑定以及数据验证" class="headerlink" title="8.2.4 在弹出窗中添加修改用户信息的表单并做响应的数据绑定以及数据验证"></a>8.2.4 在弹出窗中添加修改用户信息的表单并做响应的数据绑定以及数据验证</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 对话框主体区域 --&gt;</span><br><span class="line">&lt;el-form :model&#x3D;&quot;editForm&quot; :rules&#x3D;&quot;editFormRules&quot; ref&#x3D;&quot;editFormRef&quot; label-width&#x3D;&quot;70px&quot;&gt;</span><br><span class="line">    &lt;el-form-item label&#x3D;&quot;用户名&quot;&gt;</span><br><span class="line">        &lt;el-input v-model&#x3D;&quot;editForm.username&quot; disabled&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">    &lt;&#x2F;el-form-item&gt;</span><br><span class="line">    &lt;el-form-item label&#x3D;&quot;邮箱&quot; prop&#x3D;&quot;email&quot;&gt;</span><br><span class="line">        &lt;el-input v-model&#x3D;&quot;editForm.email&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">    &lt;&#x2F;el-form-item&gt;</span><br><span class="line">    &lt;el-form-item label&#x3D;&quot;电话&quot; prop&#x3D;&quot;mobile&quot;&gt;</span><br><span class="line">        &lt;el-input v-model&#x3D;&quot;editForm.mobile&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">    &lt;&#x2F;el-form-item&gt;</span><br><span class="line">&lt;&#x2F;el-form&gt;</span><br></pre></td></tr></table></figure>

<p>数据绑定以及验证：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;控制修改用户对话框的显示与否</span><br><span class="line">editDialogVisible: false,</span><br><span class="line">&#x2F;&#x2F;修改用户的表单数据</span><br><span class="line">editForm: &#123;</span><br><span class="line">    username: &#39;&#39;,</span><br><span class="line">    email: &#39;&#39;,</span><br><span class="line">    mobile: &#39;&#39;</span><br><span class="line">&#125;,</span><br><span class="line">&#x2F;&#x2F;修改表单的验证规则对象</span><br><span class="line">editFormRules: &#123;</span><br><span class="line">    email: [</span><br><span class="line">        &#123; required: true, message: &#39;请输入邮箱&#39;, trigger: &#39;blur&#39; &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">        validator: checkEmail,</span><br><span class="line">        message: &#39;邮箱格式不正确，请重新输入&#39;,</span><br><span class="line">        trigger: &#39;blur&#39;</span><br><span class="line">        &#125;</span><br><span class="line">    ],</span><br><span class="line">    mobile: [</span><br><span class="line">        &#123; required: true, message: &#39;请输入手机号码&#39;, trigger: &#39;blur&#39; &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">        validator: checkMobile,</span><br><span class="line">        message: &#39;手机号码不正确，请重新输入&#39;,</span><br><span class="line">        trigger: &#39;blur&#39;</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="8-2-5-监听对话框关闭事件，在对话框关闭之后，重置表单"><a href="#8-2-5-监听对话框关闭事件，在对话框关闭之后，重置表单" class="headerlink" title="8.2.5 监听对话框关闭事件，在对话框关闭之后，重置表单"></a>8.2.5 监听对话框关闭事件，在对话框关闭之后，重置表单</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;el-dialog title&#x3D;&quot;修改用户&quot; :visible.sync&#x3D;&quot;editDialogVisible&quot; width&#x3D;&quot;50%&quot; @close&#x3D;&quot;editDialogClosed&quot;&gt;</span><br><span class="line"></span><br><span class="line">editDialogClosed()&#123;</span><br><span class="line">    &#x2F;&#x2F;对话框关闭之后，重置表达</span><br><span class="line">    this.$refs.editFormRef.resetFields()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="8-2-6-在用户点击确定按钮的时候，验证数据成功之后发送请求完成修改"><a href="#8-2-6-在用户点击确定按钮的时候，验证数据成功之后发送请求完成修改" class="headerlink" title="8.2.6 在用户点击确定按钮的时候，验证数据成功之后发送请求完成修改"></a>8.2.6 在用户点击确定按钮的时候，验证数据成功之后发送请求完成修改</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">editUser() &#123;</span><br><span class="line">    &#x2F;&#x2F;用户点击修改表单中的确定按钮之后，验证表单数据</span><br><span class="line">    this.$refs.editFormRef.validate(async valid &#x3D;&gt; &#123;</span><br><span class="line">        if (!valid) return this.$message.error(&#39;请填写完整用户信息&#39;)</span><br><span class="line">        &#x2F;&#x2F;发送请求完成修改用户的操作</span><br><span class="line">        const &#123; data: res &#125; &#x3D; await this.$http.put(</span><br><span class="line">            &#39;users&#x2F;&#39; + this.editForm.id,</span><br><span class="line">            this.editForm</span><br><span class="line">        )</span><br><span class="line">        &#x2F;&#x2F;判断如果修改失败，就做提示</span><br><span class="line">        if (res.meta.status !&#x3D;&#x3D; 200) return this.$message.error(&#39;修改用户失败&#39;)</span><br><span class="line">        &#x2F;&#x2F;修改成功的提示</span><br><span class="line">        this.$message.success(&#39;修改用户成功&#39;)</span><br><span class="line">        &#x2F;&#x2F;关闭对话框</span><br><span class="line">        this.editDialogVisible &#x3D; false</span><br><span class="line">        &#x2F;&#x2F;重新请求最新的数据</span><br><span class="line">        this.getUserList()</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="8-3-删除用户"><a href="#8-3-删除用户" class="headerlink" title="8.3 删除用户"></a>8.3 删除用户</h2><p>在点击删除按钮的时候，我们应该跳出提示信息框，让用户确认要进行删除操作。<br>如果想要使用确认取消提示框，我们需要先将提示信息框挂载到vue中。</p>
<h3 id="8-3-1-导入MessageBox组件，并将MessageBox组件挂载到实例。"><a href="#8-3-1-导入MessageBox组件，并将MessageBox组件挂载到实例。" class="headerlink" title="8.3.1 导入MessageBox组件，并将MessageBox组件挂载到实例。"></a>8.3.1 导入MessageBox组件，并将MessageBox组件挂载到实例。</h3><p>Vue.prototype.$confirm = MessageBox.confirm</p>
<h3 id="8-3-2-给用户列表中的删除按钮添加事件，并在事件处理函数中弹出确定取消窗-最后再根据id发送删除用户的请求"><a href="#8-3-2-给用户列表中的删除按钮添加事件，并在事件处理函数中弹出确定取消窗-最后再根据id发送删除用户的请求" class="headerlink" title="8.3.2 给用户列表中的删除按钮添加事件，并在事件处理函数中弹出确定取消窗,最后再根据id发送删除用户的请求"></a>8.3.2 给用户列表中的删除按钮添加事件，并在事件处理函数中弹出确定取消窗,最后再根据id发送删除用户的请求</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">async removeUserById(id)&#123;</span><br><span class="line">    &#x2F;&#x2F;弹出确定取消框，是否删除用户</span><br><span class="line">    const confirmResult &#x3D; await this.$confirm(&#39;请问是否要永久删除该用户&#39;,&#39;删除提示&#39;,&#123;</span><br><span class="line">        confirmButtonText:&#39;确认删除&#39;,</span><br><span class="line">        cancelButtonText:&#39;取消&#39;,</span><br><span class="line">        type:&#39;warning&#39;</span><br><span class="line">    &#125;).catch(err&#x3D;&gt;err)</span><br><span class="line">    &#x2F;&#x2F;如果用户点击确认，则confirmResult 为&#39;confirm&#39;</span><br><span class="line">    &#x2F;&#x2F;如果用户点击取消, 则confirmResult获取的就是catch的错误消息&#39;cancel&#39;</span><br><span class="line">    if(confirmResult !&#x3D; &quot;confirm&quot;)&#123;</span><br><span class="line">        return this.$message.info(&quot;已经取消删除&quot;)</span><br><span class="line">    &#125;</span><br><span class="line">    &#x2F;&#x2F;发送请求根据id完成删除操作</span><br><span class="line">    const &#123;data:res&#125; &#x3D; await this.$http.delete(&#39;users&#x2F;&#39;+id)</span><br><span class="line">    &#x2F;&#x2F;判断如果删除失败，就做提示</span><br><span class="line">    if (res.meta.status !&#x3D;&#x3D; 200) return this.$message.error(&#39;删除用户失败&#39;)</span><br><span class="line">    &#x2F;&#x2F;修改成功的提示</span><br><span class="line">    this.$message.success(&#39;删除用户成功&#39;)</span><br><span class="line">    &#x2F;&#x2F;重新请求最新的数据</span><br><span class="line">    this.getUserList()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="8-4-推送代码"><a href="#8-4-推送代码" class="headerlink" title="8.4 推送代码"></a>8.4 推送代码</h2><p>将代码添加到暂存区：git add .</p>
<p>将代码提交并注释： git commit -m ‘添加完成用户列表功能’</p>
<p>将本地的user分支推送到码云： git push</p>
<p>将user分支代码合并到master，切换到master ：  git checkout master<br>合并user：git merge user</p>
<p>将本地master分支的代码推送到码云：git push</p>
<h1 id="9-权限功能开发"><a href="#9-权限功能开发" class="headerlink" title="9.权限功能开发"></a>9.权限功能开发</h1><h2 id="9-1-git-操作"><a href="#9-1-git-操作" class="headerlink" title="9.1 git 操作"></a>9.1 git 操作</h2><p>创建rights子分支: git checkout -b rights<br>将本地的rights分支推送到码云: git push -u origin rights</p>
<h2 id="9-2-权限列表"><a href="#9-2-权限列表" class="headerlink" title="9.2 权限列表"></a>9.2 权限列表</h2><h3 id="9-2-1-添加权限列表路由"><a href="#9-2-1-添加权限列表路由" class="headerlink" title="9.2.1 添加权限列表路由"></a>9.2.1 添加权限列表路由</h3><p>创建权限管理组件（Rights.vue），并在router.js添加对应的路由规则</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">import Rights from &#39;.&#x2F;components&#x2F;power&#x2F;Rights.vue&#39;</span><br><span class="line">......</span><br><span class="line">      path: &#39;&#x2F;home&#39;, component: Home, redirect: &#39;&#x2F;welcome&#39;, children: [</span><br><span class="line">        &#123; path: &quot;&#x2F;welcome&quot;, component: Welcome &#125;,</span><br><span class="line">        &#123; path: &quot;&#x2F;users&quot;, component: Users &#125;,</span><br><span class="line">        &#123; path: &quot;&#x2F;rights&quot;, component: Rights &#125;</span><br><span class="line">      ]</span><br><span class="line">......</span><br></pre></td></tr></table></figure>

<h3 id="9-2-2-添加面包屑导航"><a href="#9-2-2-添加面包屑导航" class="headerlink" title="9.2.2 添加面包屑导航"></a>9.2.2 添加面包屑导航</h3><p>在Rights.vue中添加面包屑组件展示导航路径</p>
<h3 id="9-2-3-显示数据"><a href="#9-2-3-显示数据" class="headerlink" title="9.2.3 显示数据"></a>9.2.3 显示数据</h3><p>在data中添加一个rightsList数据，在methods中提供一个getRightsList方法发送请求获取权限列表数据，在created中调用这个方法获取数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">&lt;el-table :data&#x3D;&quot;rightsList&quot; stripe&gt;</span><br><span class="line">    &lt;el-table-column type&#x3D;&quot;index&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">    &lt;el-table-column label&#x3D;&quot;权限名称&quot; prop&#x3D;&quot;authName&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">    &lt;el-table-column label&#x3D;&quot;路径&quot; prop&#x3D;&quot;path&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">    &lt;el-table-column label&#x3D;&quot;权限等级&quot; prop&#x3D;&quot;level&quot;&gt;</span><br><span class="line">        &lt;template slot-scope&#x3D;&quot;scope&quot;&gt; </span><br><span class="line">            &lt;el-tag v-if&#x3D;&quot;scope.row.level &#x3D;&#x3D;&#x3D; 0&quot;&gt;一级权限&lt;&#x2F;el-tag&gt;</span><br><span class="line">            &lt;el-tag v-if&#x3D;&quot;scope.row.level &#x3D;&#x3D;&#x3D; 1&quot; type&#x3D;&quot;success&quot;&gt;二级权限&lt;&#x2F;el-tag&gt;</span><br><span class="line">            &lt;el-tag v-if&#x3D;&quot;scope.row.level &#x3D;&#x3D;&#x3D; 2&quot; type&#x3D;&quot;warning&quot;&gt;三级权限&lt;&#x2F;el-tag&gt;</span><br><span class="line">        &lt;&#x2F;template&gt;</span><br><span class="line">    &lt;&#x2F;el-table-column&gt;</span><br><span class="line">&lt;&#x2F;el-table&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">    data()&#123;</span><br><span class="line">        return &#123;</span><br><span class="line">            &#x2F;&#x2F;列表形式的权限</span><br><span class="line">            rightsList:[]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    created()&#123;</span><br><span class="line">        this.getRightsList();</span><br><span class="line">    &#125;,</span><br><span class="line">    methods:&#123;</span><br><span class="line">        async getRightsList()&#123;</span><br><span class="line">            const &#123;data:res&#125; &#x3D; await this.$http.get(&#39;rights&#x2F;list&#39;)</span><br><span class="line">            &#x2F;&#x2F;如果返回状态为异常状态则报错并返回</span><br><span class="line">            if (res.meta.status !&#x3D;&#x3D; 200)</span><br><span class="line">                return this.$message.error(&#39;获取权限列表失败&#39;)</span><br><span class="line">            &#x2F;&#x2F;如果返回状态正常，将请求的数据保存在data中</span><br><span class="line">            this.rightsList &#x3D; res.data</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<h2 id="9-3-角色列表"><a href="#9-3-角色列表" class="headerlink" title="9.3 角色列表"></a>9.3 角色列表</h2><h3 id="9-3-1-添加角色列表路由"><a href="#9-3-1-添加角色列表路由" class="headerlink" title="9.3.1 添加角色列表路由"></a>9.3.1 添加角色列表路由</h3><p>添加角色列表子组件（power/Roles.vue），并添加对应的规则</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">path: &#39;&#x2F;home&#39;, component: Home, redirect: &#39;&#x2F;welcome&#39;, children: [</span><br><span class="line">        &#123; path: &quot;&#x2F;welcome&quot;, component: Welcome &#125;,</span><br><span class="line">        &#123; path: &quot;&#x2F;users&quot;, component: Users &#125;,</span><br><span class="line">        &#123; path: &quot;&#x2F;rights&quot;, component: Rights &#125;,</span><br><span class="line">        &#123; path: &quot;&#x2F;roles&quot;, component: Roles  &#125;</span><br><span class="line">      ]</span><br></pre></td></tr></table></figure>

<h3 id="9-3-2-添加面包屑导航"><a href="#9-3-2-添加面包屑导航" class="headerlink" title="9.3.2 添加面包屑导航"></a>9.3.2 添加面包屑导航</h3><p>在Roles.vue中添加面包屑组件展示导航路径</p>
<h3 id="9-3-3-显示数据"><a href="#9-3-3-显示数据" class="headerlink" title="9.3.3 显示数据"></a>9.3.3 显示数据</h3><p>在data中添加一个roleList数据，在methods中提供一个getRoleList方法发送请求获取权限列表数据，在created中调用这个方法获取数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 角色列表区域 --&gt;</span><br><span class="line">&lt;!-- row-key&#x3D;&quot;id&quot; 是2019年3月提供的新特性，</span><br><span class="line">if there&#39;s nested data, rowKey is required.</span><br><span class="line">如果这是一个嵌套的数据，rowkey 是必须添加的属性 --&gt;</span><br><span class="line">&lt;el-table row-key&#x3D;&quot;id&quot; :data&#x3D;&quot;roleList&quot; border&gt;</span><br><span class="line">    &lt;!-- 添加展开列 --&gt;</span><br><span class="line">    &lt;el-table-column type&#x3D;&quot;expand&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">    &lt;el-table-column type&#x3D;&quot;index&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">    &lt;el-table-column label&#x3D;&quot;角色名称&quot; prop&#x3D;&quot;roleName&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">    &lt;el-table-column label&#x3D;&quot;角色描述&quot; prop&#x3D;&quot;roleDesc&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">    &lt;el-table-column label&#x3D;&quot;操作&quot; width&#x3D;&quot;300px&quot;&gt;</span><br><span class="line">        &lt;template slot-scope&#x3D;&quot;scope&quot;&gt; </span><br><span class="line">            &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;primary&quot; icon&#x3D;&quot;el-icon-edit&quot;&gt;编辑&lt;&#x2F;el-button&gt;</span><br><span class="line">            &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;danger&quot; icon&#x3D;&quot;el-icon-delete&quot;&gt;删除&lt;&#x2F;el-button&gt;</span><br><span class="line">            &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;warning&quot; icon&#x3D;&quot;el-icon-setting&quot;&gt;分配权限&lt;&#x2F;el-button&gt;</span><br><span class="line">        &lt;&#x2F;template&gt;</span><br><span class="line">    &lt;&#x2F;el-table-column&gt;</span><br><span class="line">&lt;&#x2F;el-table&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">    data()&#123;</span><br><span class="line">        return &#123;</span><br><span class="line">            roleList:[]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,created()&#123;</span><br><span class="line">        this.getRoleList();</span><br><span class="line">    &#125;,methods:&#123;</span><br><span class="line">        async getRoleList()&#123;</span><br><span class="line">            const &#123;data:res&#125; &#x3D; await this.$http.get(&#39;roles&#39;)</span><br><span class="line">            &#x2F;&#x2F;如果返回状态为异常状态则报错并返回</span><br><span class="line">            &#x2F;&#x2F; if (res.meta.status !&#x3D;&#x3D; 200)</span><br><span class="line">            &#x2F;&#x2F;     return this.$message.error(&#39;获取角色列表失败&#39;)</span><br><span class="line">            &#x2F;&#x2F; &#x2F;&#x2F;如果返回状态正常，将请求的数据保存在data中</span><br><span class="line">            &#x2F;&#x2F; this.roleList &#x3D; res.data</span><br><span class="line">            console.log(res.data)</span><br><span class="line">            this.roleList &#x3D; res.data;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<h3 id="9-3-4-添加角色，删除角色，编辑角色"><a href="#9-3-4-添加角色，删除角色，编辑角色" class="headerlink" title="9.3.4 添加角色，删除角色，编辑角色"></a>9.3.4 添加角色，删除角色，编辑角色</h3><p>完成添加角色，删除角色，编辑角色三个按钮的对应功能</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;el-button type&#x3D;&quot;primary&quot; @click&#x3D;&quot;addRoleDialogVisible &#x3D; true&quot;&gt;添加角色&lt;&#x2F;el-button&gt;</span><br><span class="line">&lt;template slot-scope&#x3D;&quot;scope&quot;&gt;</span><br><span class="line">     &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;primary&quot; icon&#x3D;&quot;el-icon-edit&quot; @click&#x3D;&quot;showEditDialog(scope.row.id)&quot;&gt;编辑&lt;&#x2F;el-button&gt;</span><br><span class="line">     &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;danger&quot; icon&#x3D;&quot;el-icon-delete&quot; @click&#x3D;&quot;removeRoleById(scope.row.id)&quot;&gt;删除&lt;&#x2F;el-button&gt;</span><br><span class="line">     &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;warning&quot; icon&#x3D;&quot;el-icon-setting&quot; @click&#x3D;&quot;showSetRightDialog(scope.row)&quot;&gt;分配权限&lt;&#x2F;el-button&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br></pre></td></tr></table></figure>

<p>完成添加角色，编辑角色2个对话框</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--添加角色的对话框--&gt;</span><br><span class="line">    &lt;el-dialog title&#x3D;&quot;添加角色&quot; width&#x3D;&quot;50%&quot; :visible.sync&#x3D;&quot;addRoleDialogVisible&quot; :close-on-click-modal&#x3D;&quot;false&quot; @close&#x3D;&quot;addRoleDialogClosed&quot;&gt;</span><br><span class="line">      &lt;!--内容主体--&gt;</span><br><span class="line">      &lt;el-form :model&#x3D;&quot;addRoleForm&quot; :rules&#x3D;&quot;addRoleFormRules&quot; ref&#x3D;&quot;addRoleFormRef&quot; label-width&#x3D;&quot;80px&quot;&gt;</span><br><span class="line">        &lt;el-form-item label&#x3D;&quot;角色名称&quot; prop&#x3D;&quot;roleName&quot;&gt;</span><br><span class="line">          &lt;el-input v-model&#x3D;&quot;addRoleForm.roleName&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">        &lt;&#x2F;el-form-item&gt;</span><br><span class="line">        &lt;el-form-item label&#x3D;&quot;角色描述&quot; prop&#x3D;&quot;roleDesc&quot;&gt;</span><br><span class="line">          &lt;el-input v-model&#x3D;&quot;addRoleForm.roleDesc&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">        &lt;&#x2F;el-form-item&gt;</span><br><span class="line">      &lt;&#x2F;el-form&gt;</span><br><span class="line">      &lt;!--底部--&gt;</span><br><span class="line">      &lt;span slot&#x3D;&quot;footer&quot;&gt;</span><br><span class="line">        &lt;el-button @click&#x3D;&quot;addRoleDialogVisible &#x3D; false&quot;&gt;取 消&lt;&#x2F;el-button&gt;</span><br><span class="line">        &lt;el-button type&#x3D;&quot;primary&quot; @click&#x3D;&quot;addRole&quot;&gt;确 定&lt;&#x2F;el-button&gt;</span><br><span class="line">      &lt;&#x2F;span&gt;</span><br><span class="line">    &lt;&#x2F;el-dialog&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!--修改角色的对话框--&gt;</span><br><span class="line">    &lt;el-dialog title&#x3D;&quot;修改角色&quot; width&#x3D;&quot;50%&quot; :visible.sync&#x3D;&quot;editRoleDialogVisible&quot; :close-on-click-modal&#x3D;&quot;false&quot; @close&#x3D;&quot;editRoleDialogClosed&quot;&gt;</span><br><span class="line">      &lt;!--内容主体--&gt;</span><br><span class="line">      &lt;el-form :model&#x3D;&quot;editRoleForm&quot; :rules&#x3D;&quot;editRoleFormRules&quot; ref&#x3D;&quot;editRoleFormRef&quot; label-width&#x3D;&quot;80px&quot;&gt;</span><br><span class="line">        &lt;el-form-item label&#x3D;&quot;角色名称&quot; prop&#x3D;&quot;roleName&quot;&gt;</span><br><span class="line">          &lt;el-input v-model&#x3D;&quot;editRoleForm.roleName&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">        &lt;&#x2F;el-form-item&gt;</span><br><span class="line">        &lt;el-form-item label&#x3D;&quot;角色描述&quot; prop&#x3D;&quot;roleDesc&quot;&gt;</span><br><span class="line">          &lt;el-input v-model&#x3D;&quot;editRoleForm.roleDesc&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">        &lt;&#x2F;el-form-item&gt;</span><br><span class="line">      &lt;&#x2F;el-form&gt;</span><br><span class="line">      &lt;!--底部--&gt;</span><br><span class="line">      &lt;span slot&#x3D;&quot;footer&quot;&gt;</span><br><span class="line">        &lt;el-button @click&#x3D;&quot;editRoleDialogVisible &#x3D; false&quot;&gt;取 消&lt;&#x2F;el-button&gt;</span><br><span class="line">        &lt;el-button type&#x3D;&quot;primary&quot; @click&#x3D;&quot;editRole&quot;&gt;确 定&lt;&#x2F;el-button&gt;</span><br><span class="line">      &lt;&#x2F;span&gt;</span><br><span class="line">    &lt;&#x2F;el-dialog&gt;</span><br></pre></td></tr></table></figure>

<p>实现3个功能</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br></pre></td><td class="code"><pre><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      &#x2F;&#x2F; 添加角色对话框的显示状态</span><br><span class="line">      addRoleDialogVisible: false,</span><br><span class="line">      &#x2F;&#x2F; 修改角色对话框的显示状态</span><br><span class="line">      editRoleDialogVisible: false,</span><br><span class="line">      &#x2F;&#x2F; 添加角色的表单数据</span><br><span class="line">      addRoleForm: &#123;</span><br><span class="line">        roleName: &#39;&#39;,</span><br><span class="line">        roleDesc: &#39;&#39;</span><br><span class="line">      &#125;,</span><br><span class="line">      &#x2F;&#x2F; 添加角色表单的验证规则对象</span><br><span class="line">      addRoleFormRules: &#123;</span><br><span class="line">        roleName: [&#123; required: true, message: &#39;请输入角色名称&#39;, trigger: &#39;blur&#39; &#125;],</span><br><span class="line">        roleDesc: [&#123; required: true, message: &#39;请输入角色描述&#39;, trigger: &#39;blur&#39; &#125;]</span><br><span class="line">      &#125;,</span><br><span class="line">      &#x2F;&#x2F; 修改角色的表单数据</span><br><span class="line">      editRoleForm: &#123;</span><br><span class="line">        roleName: &#39;&#39;,</span><br><span class="line">        roleDesc: &#39;&#39;</span><br><span class="line">      &#125;,</span><br><span class="line">      &#x2F;&#x2F; 修改角色表单的验证规则对象</span><br><span class="line">      editRoleFormRules: &#123;</span><br><span class="line">        roleName: [&#123; required: true, message: &#39;请输入角色名称&#39;, trigger: &#39;blur&#39; &#125;],</span><br><span class="line">        roleDesc: [&#123; required: true, message: &#39;请输入角色描述&#39;, trigger: &#39;blur&#39; &#125;]</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    &#x2F;&#x2F; 添加角色对话框的关闭事件</span><br><span class="line">    addRoleDialogClosed() &#123;</span><br><span class="line">      this.$refs.addRoleFormRef.resetFields()</span><br><span class="line">    &#125;,</span><br><span class="line">    &#x2F;&#x2F; 确认添加角色</span><br><span class="line">    addRole() &#123;</span><br><span class="line">      this.$refs.addRoleFormRef.validate(async valid &#x3D;&gt; &#123;</span><br><span class="line">        if (!valid) &#123;</span><br><span class="line">          return this.$message.error(&#39;请填写必要的角色信息!&#39;)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        &#x2F;&#x2F; 发起请求添加角色</span><br><span class="line">        &#x2F;&#x2F; 商品的名称必须是唯一的</span><br><span class="line">        const &#123; data: res &#125; &#x3D; await this.$http.post(&#39;roles&#39;, this.addRoleForm)</span><br><span class="line">        if (res.meta.status !&#x3D;&#x3D; 201) &#123;</span><br><span class="line">          return this.$message.error(&#39;添加角色失败!&#39;)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        this.getRolesList()</span><br><span class="line">        this.$message.success(&#39;添加角色成功!&#39;)</span><br><span class="line">        this.addRoleDialogVisible &#x3D; false</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;,</span><br><span class="line">    &#x2F;&#x2F; 展示编辑角色的对话框</span><br><span class="line">    async showEditDialog(id) &#123;</span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.get(&#96;roles&#x2F;$&#123;id&#125;&#96;)</span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">        return this.$message.error(&#39;查询角色信息失败！&#39;)</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      this.editRoleForm &#x3D; res.data</span><br><span class="line">      &#x2F;&#x2F; console.log(this.editRoleForm)</span><br><span class="line">      this.editRoleDialogVisible &#x3D; true</span><br><span class="line">    &#125;,</span><br><span class="line">    &#x2F;&#x2F; 修改角色对话框的关闭事件</span><br><span class="line">    editRoleDialogClosed() &#123;</span><br><span class="line">      this.$refs.editRoleFormRef.resetFields()</span><br><span class="line">    &#125;,</span><br><span class="line">    &#x2F;&#x2F; 确认修改角色</span><br><span class="line">    editRole() &#123;</span><br><span class="line">      this.$refs.editRoleFormRef.validate(async valid &#x3D;&gt; &#123;</span><br><span class="line">        if (!valid) return</span><br><span class="line">        &#x2F;&#x2F; console.log(this.editRoleForm.roleId)</span><br><span class="line">        const &#123; data: res &#125; &#x3D; await this.$http.put(&#96;roles&#x2F;$&#123;this.editRoleForm.roleId&#125;&#96;, this.editRoleForm)</span><br><span class="line"></span><br><span class="line">        if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">          return this.$message.error(&#39;编辑角色失败!&#39;)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        this.getRolesList()</span><br><span class="line">        this.$message.success(&#39;编辑角色成功!&#39;)</span><br><span class="line">        &#x2F;&#x2F; 隐藏对话框</span><br><span class="line">        this.editRoleDialogVisible &#x3D; false</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;,</span><br><span class="line">    &#x2F;&#x2F; 根据id删除对应的角色</span><br><span class="line">    async removeRoleById(id) &#123;</span><br><span class="line">      const confirmResult &#x3D; await this.$confirm(&#39;此操作将永久删除该角色, 是否继续?&#39;, &#39;提示&#39;, &#123;</span><br><span class="line">        confirmButtonText: &#39;确定&#39;,</span><br><span class="line">        cancelButtonText: &#39;取消&#39;,</span><br><span class="line">        type: &#39;warning&#39;</span><br><span class="line">      &#125;).catch(err &#x3D;&gt; err)</span><br><span class="line"></span><br><span class="line">      &#x2F;&#x2F; 如果用户确认删除，则返回值为字符串 confirm</span><br><span class="line">      &#x2F;&#x2F; 如果用户取消了删除，则返回值为字符串 cancel</span><br><span class="line">      &#x2F;&#x2F; console.log(confirmResult)</span><br><span class="line">      if (confirmResult !&#x3D;&#x3D; &#39;confirm&#39;) &#123;</span><br><span class="line">        return this.$message.info(&#39;已取消删除&#39;)</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.delete(&#96;roles&#x2F;$&#123;id&#125;&#96;)</span><br><span class="line"></span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">        return this.$message.error(&#39;删除角色失败！&#39;)</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      this.$message.success(&#39;删除角色成功！&#39;)</span><br><span class="line">      this.getRolesList()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<h3 id="9-3-5-生成权限列表"><a href="#9-3-5-生成权限列表" class="headerlink" title="9.3.5 生成权限列表"></a>9.3.5 生成权限列表</h3><p>使用三重嵌套for循环生成权限下拉列表</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 添加展开列 --&gt;</span><br><span class="line">&lt;el-table-column type&#x3D;&quot;expand&quot;&gt;</span><br><span class="line">    &lt;template slot-scope&#x3D;&quot;scope&quot;&gt;</span><br><span class="line">        &lt;el-row :class&#x3D;&quot;[&#39;bdbottom&#39;,i1&#x3D;&#x3D;&#x3D;0?&#39;bdtop&#39;:&#39;&#39;]&quot; v-for&#x3D;&quot;(item1,i1) in scope.row.children&quot; :key&#x3D;&quot;item1.id&quot;&gt;</span><br><span class="line">            &lt;!-- 渲染一级权限 --&gt;</span><br><span class="line">            &lt;el-col :span&#x3D;&quot;5&quot;&gt;</span><br><span class="line">                &lt;el-tag&gt;</span><br><span class="line">                    &#123;&#123;item1.authName&#125;&#125;</span><br><span class="line">                &lt;&#x2F;el-tag&gt;</span><br><span class="line">                &lt;i class&#x3D;&quot;el-icon-caret-right&quot;&gt;&lt;&#x2F;i&gt;</span><br><span class="line">            &lt;&#x2F;el-col&gt;</span><br><span class="line">            &lt;!-- 渲染二，三级权限 --&gt;</span><br><span class="line">            &lt;el-col :span&#x3D;&quot;19&quot;&gt;</span><br><span class="line">                &lt;!-- 通过for循环嵌套渲染二级权限  --&gt;</span><br><span class="line">                &lt;el-row :class&#x3D;&quot;[i2&#x3D;&#x3D;&#x3D;0?&#39;&#39;:&#39;bdtop&#39; ]&quot; v-for&#x3D;&quot;(item2,i2) in item1.children&quot; :key&#x3D;&quot;item2.id&quot;&gt;</span><br><span class="line">                    &lt;el-col :span&#x3D;&quot;6&quot;&gt;</span><br><span class="line">                        &lt;el-tag type&#x3D;&quot;success&quot;&gt;&#123;&#123;item2.authName&#125;&#125;&lt;&#x2F;el-tag&gt;</span><br><span class="line">                        &lt;i class&#x3D;&quot;el-icon-caret-right&quot;&gt;&lt;&#x2F;i&gt;</span><br><span class="line">                    &lt;&#x2F;el-col&gt;</span><br><span class="line">                    &lt;el-col :span&#x3D;&quot;18&quot;&gt;</span><br><span class="line">                        &lt;el-tag type&#x3D;&quot;warning&quot; v-for&#x3D;&quot;(item3) in item2.children&quot; :key&#x3D;&quot;item3.id&quot;&gt;</span><br><span class="line">                            &#123;&#123;item3.authName&#125;&#125;</span><br><span class="line">                        &lt;&#x2F;el-tag&gt;</span><br><span class="line">                    &lt;&#x2F;el-col&gt;</span><br><span class="line">                &lt;&#x2F;el-row&gt;</span><br><span class="line">            &lt;&#x2F;el-col&gt;</span><br><span class="line">        &lt;&#x2F;el-row&gt;</span><br><span class="line">    &lt;&#x2F;template&gt;</span><br><span class="line">&lt;&#x2F;el-table-column&gt;</span><br></pre></td></tr></table></figure>

<h3 id="9-3-6-美化样式"><a href="#9-3-6-美化样式" class="headerlink" title="9.3.6 美化样式"></a>9.3.6 美化样式</h3><p>通过设置global.css中的#app样式min-width:1366px 解决三级权限换行的问题<br>，通过给一级权限el-row添加display:flex,align-items:center的方式解决一级权限垂直居中的问题，二级权限也类似添加，因为需要给多个内容添加，可以将这个样式设置为一个.vcenter{display:flex;align-items:center}</p>
<h3 id="9-3-7-添加权限删除功能"><a href="#9-3-7-添加权限删除功能" class="headerlink" title="9.3.7 添加权限删除功能"></a>9.3.7 添加权限删除功能</h3><p>给每一个权限的el-tag添加closable属性，是的权限右侧出现“X”图标<br>再给el-tag添加绑定close事件处理函数removeRightById(scope.row,item1.id)<br>removeRightById(scope.row,item2.id)<br>removeRightById(scope.row,item3.id)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">async removeRightById(role,rightId)&#123;</span><br><span class="line">    &#x2F;&#x2F; 弹窗提示用户是否要删除</span><br><span class="line">    const confirmResult &#x3D; await this.$confirm(&#39;请问是否要删除该权限&#39;,&#39;删除提示&#39;,&#123;</span><br><span class="line">        confirmButtonText:&#39;确认删除&#39;,</span><br><span class="line">        cancelButtonText:&#39;取消&#39;,</span><br><span class="line">        type:&#39;warning&#39;</span><br><span class="line">    &#125;).catch(err&#x3D;&gt;err)</span><br><span class="line">    &#x2F;&#x2F; 如果用户点击确认，则confirmResult 为&#39;confirm&#39;</span><br><span class="line">    &#x2F;&#x2F; 如果用户点击取消, 则confirmResult获取的就是catch的错误消息&#39;cancel&#39;</span><br><span class="line">    if(confirmResult !&#x3D; &quot;confirm&quot;)&#123;</span><br><span class="line">        return this.$message.info(&quot;已经取消删除&quot;)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    &#x2F;&#x2F; 用户点击了确定表示真的要删除</span><br><span class="line">    &#x2F;&#x2F; 当发送delete请求之后，返回的数据就是最新的角色权限信息</span><br><span class="line">    const &#123;data:res&#125; &#x3D; await this.$http.delete(&#96;roles&#x2F;$&#123;role.id&#125;&#x2F;rights&#x2F;$&#123;rightId&#125;&#96;)</span><br><span class="line">    if (res.meta.status !&#x3D;&#x3D; 200)</span><br><span class="line">        return this.$message.error(&#39;删除角色权限失败&#39;)</span><br><span class="line"></span><br><span class="line">    &#x2F;&#x2F; 无需再重新加载所有权限</span><br><span class="line">    &#x2F;&#x2F;只需要对现有的角色权限进行更新即可</span><br><span class="line">    role.children &#x3D; res.data</span><br><span class="line">    &#x2F;&#x2F; this.getRoleList();</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="9-3-8-完成权限分配功能"><a href="#9-3-8-完成权限分配功能" class="headerlink" title="9.3.8 完成权限分配功能"></a>9.3.8 完成权限分配功能</h3><p>先给分配权限按钮添加事件<br>&lt;el-button size=”mini” type=”warning” icon=”el-icon-setting” @click=”showSetRightDialog”&gt;分配权限</el-button><br>在showSetRightDialog函数中请求权限树数据并显示对话框</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">async showSetRightDialog() &#123;</span><br><span class="line">    &#x2F;&#x2F;当点击分配权限按钮时，展示对应的对话框</span><br><span class="line">    this.setRightDialogVisible &#x3D; true;</span><br><span class="line">    &#x2F;&#x2F;获取所有权限的数据</span><br><span class="line">    const &#123;data:res&#125; &#x3D; await this.$http.get(&#39;rights&#x2F;tree&#39;)</span><br><span class="line">    &#x2F;&#x2F;如果返回状态为异常状态则报错并返回</span><br><span class="line">    if (res.meta.status !&#x3D;&#x3D; 200)</span><br><span class="line">        return this.$message.error(&#39;获取权限树失败&#39;)</span><br><span class="line">    &#x2F;&#x2F;如果返回状态正常，将请求的数据保存在data中</span><br><span class="line">    this.rightsList &#x3D; res.data</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>添加分配权限对话框，并添加绑定数据setRightDialogVisible</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 分配权限对话框 --&gt;</span><br><span class="line">&lt;el-dialog title&#x3D;&quot;分配权限&quot; :visible.sync&#x3D;&quot;setRightDialogVisible&quot; width&#x3D;&quot;50%&quot;&gt;</span><br><span class="line">    &lt;span&gt;这是一段信息&lt;&#x2F;span&gt;</span><br><span class="line">    &lt;span slot&#x3D;&quot;footer&quot; class&#x3D;&quot;dialog-footer&quot;&gt;</span><br><span class="line">        &lt;el-button @click&#x3D;&quot;setRightDialogVisible &#x3D; false&quot;&gt;取 消&lt;&#x2F;el-button&gt;</span><br><span class="line">        &lt;el-button type&#x3D;&quot;primary&quot; @click&#x3D;&quot;setRightDialogVisible &#x3D; false&quot;&gt;确 定&lt;&#x2F;el-button&gt;</span><br><span class="line">    &lt;&#x2F;span&gt;</span><br><span class="line">&lt;&#x2F;el-dialog&gt;</span><br></pre></td></tr></table></figure>

<h3 id="9-3-9-完成树形结构弹窗"><a href="#9-3-9-完成树形结构弹窗" class="headerlink" title="9.3.9 完成树形结构弹窗"></a>9.3.9 完成树形结构弹窗</h3><p>在element.js中引入Tree，注册Tree</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 分配权限对话框 --&gt;</span><br><span class="line">&lt;el-dialog title&#x3D;&quot;分配权限&quot; :visible.sync&#x3D;&quot;setRightDialogVisible&quot; width&#x3D;&quot;50%&quot; @close&#x3D;&quot;setRightDialogClose&quot;&gt;</span><br><span class="line">    &lt;!-- 树形组件</span><br><span class="line">    show-checkbox:显示复选框</span><br><span class="line">    node-key:设置选中节点对应的值</span><br><span class="line">    default-expand-all:是否默认展开所有节点</span><br><span class="line">    :default-checked-keys 设置默认选中项的数组</span><br><span class="line">    ref:设置引用 --&gt;</span><br><span class="line">    &lt;el-tree :data&#x3D;&quot;rightsList&quot; :props&#x3D;&quot;treeProps&quot; show-checkbox node-key&#x3D;&quot;id&quot; default-expand-all :default-checked-keys&#x3D;&quot;defKeys&quot; ref&#x3D;&quot;treeRef&quot;&gt;&lt;&#x2F;el-tree&gt;</span><br><span class="line">    &lt;span slot&#x3D;&quot;footer&quot; class&#x3D;&quot;dialog-footer&quot;&gt;</span><br><span class="line">        &lt;el-button @click&#x3D;&quot;setRightDialogVisible &#x3D; false&quot;&gt;取 消&lt;&#x2F;el-button&gt;</span><br><span class="line">        &lt;el-button type&#x3D;&quot;primary&quot; @click&#x3D;&quot;allotRights&quot;&gt;确 定&lt;&#x2F;el-button&gt;</span><br><span class="line">    &lt;&#x2F;span&gt;</span><br><span class="line">&lt;&#x2F;el-dialog&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      &#x2F;&#x2F; 角色列表数据</span><br><span class="line">      roleList: [],</span><br><span class="line">      &#x2F;&#x2F; 控制分配权限对话框的显示</span><br><span class="line">      setRightDialogVisible: false,</span><br><span class="line">      &#x2F;&#x2F; 权限树数据</span><br><span class="line">      rightsList: [],</span><br><span class="line">      &#x2F;&#x2F; 树形控件的属性绑定对象</span><br><span class="line">      treeProps: &#123;</span><br><span class="line">        &#x2F;&#x2F;通过label设置树形节点文本展示authName</span><br><span class="line">        label: &#39;authName&#39;,</span><br><span class="line">        &#x2F;&#x2F;设置通过children属性展示子节点信息</span><br><span class="line">        children: &#39;children&#39;</span><br><span class="line">      &#125;,</span><br><span class="line">      &#x2F;&#x2F; 设置树形控件中默认选中的内容</span><br><span class="line">      defKeys: [],</span><br><span class="line">      &#x2F;&#x2F; 保存正在操作的角色id</span><br><span class="line">      roleId:&#39;&#39;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  created() &#123;</span><br><span class="line">    this.getRoleList()</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    async getRoleList() &#123;</span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.get(&#39;roles&#39;)</span><br><span class="line">      &#x2F;&#x2F; 如果返回状态为异常状态则报错并返回</span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200)</span><br><span class="line">        return this.$message.error(&#39;获取角色列表失败&#39;)</span><br><span class="line">      &#x2F;&#x2F; 如果返回状态正常，将请求的数据保存在data中</span><br><span class="line">      &#x2F;&#x2F; this.roleList &#x3D; res.data</span><br><span class="line">      console.log(res.data)</span><br><span class="line">      this.roleList &#x3D; res.data</span><br><span class="line">    &#125;,</span><br><span class="line">    async removeRightById(role, rightId) &#123;</span><br><span class="line">      &#x2F;&#x2F; 弹窗提示用户是否要删除</span><br><span class="line">      const confirmResult &#x3D; await this.$confirm(</span><br><span class="line">        &#39;请问是否要删除该权限&#39;,</span><br><span class="line">        &#39;删除提示&#39;,</span><br><span class="line">        &#123;</span><br><span class="line">          confirmButtonText: &#39;确认删除&#39;,</span><br><span class="line">          cancelButtonText: &#39;取消&#39;,</span><br><span class="line">          type: &#39;warning&#39;</span><br><span class="line">        &#125;</span><br><span class="line">      ).catch(err &#x3D;&gt; err)</span><br><span class="line">      &#x2F;&#x2F; 如果用户点击确认，则confirmResult 为&#39;confirm&#39;</span><br><span class="line">      &#x2F;&#x2F; 如果用户点击取消, 则confirmResult获取的就是catch的错误消息&#39;cancel&#39;</span><br><span class="line">      if (confirmResult !&#x3D; &#39;confirm&#39;) &#123;</span><br><span class="line">        return this.$message.info(&#39;已经取消删除&#39;)</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      &#x2F;&#x2F; 用户点击了确定表示真的要删除</span><br><span class="line">      &#x2F;&#x2F; 当发送delete请求之后，返回的数据就是最新的角色权限信息</span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.delete(</span><br><span class="line">        &#96;roles&#x2F;$&#123;role.id&#125;&#x2F;rights&#x2F;$&#123;rightId&#125;&#96;</span><br><span class="line">      )</span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200)</span><br><span class="line">        return this.$message.error(&#39;删除角色权限失败&#39;)</span><br><span class="line"></span><br><span class="line">      &#x2F;&#x2F; 无需再重新加载所有权限</span><br><span class="line">      &#x2F;&#x2F; 只需要对现有的角色权限进行更新即可</span><br><span class="line">      role.children &#x3D; res.data</span><br><span class="line">      &#x2F;&#x2F; this.getRoleList();</span><br><span class="line">    &#125;,</span><br><span class="line">    async showSetRightDialog(role) &#123;</span><br><span class="line">      &#x2F;&#x2F; 将role.id保存起来以供保存权限时使用</span><br><span class="line">      this.roleId &#x3D; role.id;  </span><br><span class="line">      &#x2F;&#x2F; 获取所有权限的数据</span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.get(&#39;rights&#x2F;tree&#39;)</span><br><span class="line">      &#x2F;&#x2F; 如果返回状态为异常状态则报错并返回</span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200) return this.$message.error(&#39;获取权限树失败&#39;)</span><br><span class="line">      &#x2F;&#x2F; 如果返回状态正常，将请求的数据保存在data中</span><br><span class="line">      this.rightsList &#x3D; res.data</span><br><span class="line"></span><br><span class="line">      &#x2F;&#x2F; 调用getLeafKeys进行递归，将三级权限添加到数组中</span><br><span class="line">      this.getLeafKeys(role, this.defKeys)</span><br><span class="line">      &#x2F;&#x2F; 当点击分配权限按钮时，展示对应的对话框</span><br><span class="line">      this.setRightDialogVisible &#x3D; true</span><br><span class="line">      console.log(this.defKeys)</span><br><span class="line">    &#125;,</span><br><span class="line">    getLeafKeys(node, arr) &#123;</span><br><span class="line">      &#x2F;&#x2F; 该函数会获取到当前角色的所有三级权限id并添加到defKeys中</span><br><span class="line">      &#x2F;&#x2F; 如果当前节点不包含children属性，则表示node为三级权限</span><br><span class="line">      if (!node.children) &#123;</span><br><span class="line">        return arr.push(node.id)</span><br><span class="line">      &#125;</span><br><span class="line">      &#x2F;&#x2F; 递归调用</span><br><span class="line">      node.children.forEach(item &#x3D;&gt; this.getLeafKeys(item, arr))</span><br><span class="line">    &#125;,</span><br><span class="line">    setRightDialogClose() &#123;</span><br><span class="line">      &#x2F;&#x2F; 当用户关闭树形权限对话框的时候，清除掉所有选中状态</span><br><span class="line">      this.defKeys &#x3D; []</span><br><span class="line">    &#125;,</span><br><span class="line">    async allotRights() &#123;</span><br><span class="line">      &#x2F;&#x2F; 当用户在树形权限对话框中点击确定，将用户选择的</span><br><span class="line">      &#x2F;&#x2F; 权限发送请求进行更新</span><br><span class="line"></span><br><span class="line">      &#x2F;&#x2F; 获取所有选中及半选的内容</span><br><span class="line">      const keys &#x3D; [</span><br><span class="line">        ...this.$refs.treeRef.getCheckedKeys(),</span><br><span class="line">        ...this.$refs.treeRef.getHalfCheckedKeys()</span><br><span class="line">      ]</span><br><span class="line">      &#x2F;&#x2F; 将数组转换为 , 拼接的字符串</span><br><span class="line">      const idStr &#x3D; keys.join(&#39;,&#39;)</span><br><span class="line"></span><br><span class="line">      &#x2F;&#x2F; 发送请求完成更新</span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.post(</span><br><span class="line">        &#96;roles&#x2F;$&#123;this.roleId&#125;&#x2F;rights&#96;,</span><br><span class="line">        &#123; rids:idStr &#125;</span><br><span class="line">      )</span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200)</span><br><span class="line">        return this.$message.error(&#39;分配权限失败&#39;)</span><br><span class="line"></span><br><span class="line">      this.$message.success(&quot;分配权限成功&quot;)</span><br><span class="line">      this.getRoleList();</span><br><span class="line">      &#x2F;&#x2F; 关闭对话框</span><br><span class="line">      this.setRightDialogVisible &#x3D; false;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<h2 id="9-4-分配角色"><a href="#9-4-分配角色" class="headerlink" title="9.4 分配角色"></a>9.4 分配角色</h2><p>打开Users.vue，完成分配角色的功能</p>
<h3 id="9-4-1-添加分配角色对话框"><a href="#9-4-1-添加分配角色对话框" class="headerlink" title="9.4.1 添加分配角色对话框"></a>9.4.1 添加分配角色对话框</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 分配角色对话框 --&gt;</span><br><span class="line">&lt;el-dialog title&#x3D;&quot;分配角色&quot; :visible.sync&#x3D;&quot;setRoleDialogVisible&quot; width&#x3D;&quot;50%&quot;&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">    &lt;p&gt;当前的用户:&#123;&#123;userInfo.username&#125;&#125;&lt;&#x2F;p&gt;</span><br><span class="line">    &lt;p&gt;当前的角色:&#123;&#123;userInfo.role_name&#125;&#125;&lt;&#x2F;p&gt;</span><br><span class="line">    &lt;p&gt;分配新角色:&lt;&#x2F;p&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">    &lt;span slot&#x3D;&quot;footer&quot; class&#x3D;&quot;dialog-footer&quot;&gt;</span><br><span class="line">    &lt;el-button @click&#x3D;&quot;setRoleDialogVisible &#x3D; false&quot;&gt;取 消&lt;&#x2F;el-button&gt;</span><br><span class="line">    &lt;el-button type&#x3D;&quot;primary&quot; @click&#x3D;&quot;setRoleDialogVisible &#x3D; false&quot;&gt;确 定&lt;&#x2F;el-button&gt;</span><br><span class="line">    &lt;&#x2F;span&gt;</span><br><span class="line">&lt;&#x2F;el-dialog&gt;</span><br></pre></td></tr></table></figure>

<h3 id="9-4-2-给分配角色按钮添加点击事件，点击之后弹出一个对话框进行角色分配"><a href="#9-4-2-给分配角色按钮添加点击事件，点击之后弹出一个对话框进行角色分配" class="headerlink" title="9.4.2 给分配角色按钮添加点击事件，点击之后弹出一个对话框进行角色分配"></a>9.4.2 给分配角色按钮添加点击事件，点击之后弹出一个对话框进行角色分配</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 分配角色 --&gt;</span><br><span class="line">&lt;el-tooltip class&#x3D;&quot;item&quot; effect&#x3D;&quot;dark&quot; content&#x3D;&quot;分配角色&quot; placement&#x3D;&quot;top&quot; :enterable&#x3D;&quot;false&quot;&gt;</span><br><span class="line">    &lt;el-button type&#x3D;&quot;warning&quot; icon&#x3D;&quot;el-icon-setting&quot; size&#x3D;&#39;mini&#39; @click&#x3D;&quot;setRole(scope.row)&quot;&gt;&lt;&#x2F;el-button&gt;</span><br><span class="line">&lt;&#x2F;el-tooltip&gt;</span><br><span class="line"></span><br><span class="line">data()&#123;</span><br><span class="line">    ......</span><br><span class="line">    &#x2F;&#x2F;控制显示分配角色对话框</span><br><span class="line">    setRoleDialogVisible:false,</span><br><span class="line">    &#x2F;&#x2F;保存正在操作的那个用户信息</span><br><span class="line">    userInfo:&#123;&#125;,</span><br><span class="line">    &#x2F;&#x2F;保存所有的角色信息</span><br><span class="line">    rolesList:[],</span><br><span class="line">    &#x2F;&#x2F;保存用户选中的角色id</span><br><span class="line">    selectedRoleId:&#39;&#39;</span><br><span class="line">&#125;,</span><br><span class="line">methods:&#123;</span><br><span class="line">    ......</span><br><span class="line">    async setRole( userInfo )&#123;</span><br><span class="line">      &#x2F;&#x2F;保存起来以供后续使用</span><br><span class="line">      this.userInfo &#x3D; userInfo;</span><br><span class="line">      &#x2F;&#x2F;获取所有的角色信息，以备下拉列表使用</span><br><span class="line">      &#x2F;&#x2F;发送请求根据id完成删除操作</span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.get(&#39;roles&#39;)</span><br><span class="line">      &#x2F;&#x2F;判断如果删除失败，就做提示</span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200) return this.$message.error(&#39;获取角色列表失败&#39;)</span><br><span class="line">      </span><br><span class="line">      this.rolesList &#x3D; res.data;</span><br><span class="line">      &#x2F;&#x2F;展示分配角色对话框</span><br><span class="line">      this.setRoleDialogVisible &#x3D; true;</span><br><span class="line"></span><br><span class="line">      </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="9-4-3-在element-js中引入Select，Option，注册Select，Option"><a href="#9-4-3-在element-js中引入Select，Option，注册Select，Option" class="headerlink" title="9.4.3 在element.js中引入Select，Option，注册Select，Option"></a>9.4.3 在element.js中引入Select，Option，注册Select，Option</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 角色选择下拉框</span><br><span class="line">v-model：设置用户选中角色之后的id绑定数据</span><br><span class="line">--&gt;</span><br><span class="line">&lt;el-select v-model&#x3D;&quot;selectedRoleId&quot; placeholder&#x3D;&quot;请选择角色&quot;&gt;</span><br><span class="line">&lt;!-- :label 显示文本，:value 选中值 --&gt;</span><br><span class="line">&lt;el-option v-for&#x3D;&quot;item in rolesList&quot; :key&#x3D;&quot;item.id&quot; :label&#x3D;&quot;item.roleName&quot; :value&#x3D;&quot;item.id&quot;&gt;</span><br><span class="line">&lt;&#x2F;el-option&gt;</span><br><span class="line">&lt;&#x2F;el-select&gt;</span><br></pre></td></tr></table></figure>

<h3 id="9-4-4-当用户点击对话框中的确定之后，完成分配角色的操作"><a href="#9-4-4-当用户点击对话框中的确定之后，完成分配角色的操作" class="headerlink" title="9.4.4 当用户点击对话框中的确定之后，完成分配角色的操作"></a>9.4.4 当用户点击对话框中的确定之后，完成分配角色的操作</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 分配角色对话框 --&gt;</span><br><span class="line">&lt;el-dialog title&#x3D;&quot;分配角色&quot; :visible.sync&#x3D;&quot;setRoleDialogVisible&quot; width&#x3D;&quot;50%&quot; @close&#x3D;&quot;setRoleDialogClosed&quot;&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">    &lt;p&gt;当前的用户:&#123;&#123;userInfo.username&#125;&#125;&lt;&#x2F;p&gt;</span><br><span class="line">    &lt;p&gt;当前的角色:&#123;&#123;userInfo.role_name&#125;&#125;&lt;&#x2F;p&gt;</span><br><span class="line">    &lt;p&gt;分配新角色:</span><br><span class="line">        &lt;!-- 角色选择下拉框</span><br><span class="line">        v-model：设置用户选中角色之后的id绑定数据</span><br><span class="line">        --&gt;</span><br><span class="line">        &lt;el-select v-model&#x3D;&quot;selectedRoleId&quot; placeholder&#x3D;&quot;请选择角色&quot;&gt;</span><br><span class="line">        &lt;!-- :label 显示文本，:value 选中值 --&gt;</span><br><span class="line">        &lt;el-option v-for&#x3D;&quot;item in rolesList&quot; :key&#x3D;&quot;item.id&quot; :label&#x3D;&quot;item.roleName&quot; :value&#x3D;&quot;item.id&quot;&gt;</span><br><span class="line">        &lt;&#x2F;el-option&gt;</span><br><span class="line">        &lt;&#x2F;el-select&gt;</span><br><span class="line">    &lt;&#x2F;p&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">    &lt;span slot&#x3D;&quot;footer&quot; class&#x3D;&quot;dialog-footer&quot;&gt;</span><br><span class="line">    &lt;el-button @click&#x3D;&quot;setRoleDialogVisible &#x3D; false&quot;&gt;取 消&lt;&#x2F;el-button&gt;</span><br><span class="line">    &lt;el-button type&#x3D;&quot;primary&quot; @click&#x3D;&quot;saveRoleInfo&quot;&gt;确 定&lt;&#x2F;el-button&gt;</span><br><span class="line">    &lt;&#x2F;span&gt;</span><br><span class="line">&lt;&#x2F;el-dialog&gt;</span><br><span class="line"></span><br><span class="line">methods:&#123;</span><br><span class="line">    .......</span><br><span class="line">    async saveRoleInfo()&#123;</span><br><span class="line">      &#x2F;&#x2F;当用户点击确定按钮之后</span><br><span class="line">      &#x2F;&#x2F;判断用户是否选择了需要分配的角色</span><br><span class="line">      if(!this.selectedRoleId)&#123;</span><br><span class="line">        return this.$message.error(&#39;请选择需要分配的角色&#39;)</span><br><span class="line">      &#125;</span><br><span class="line">      &#x2F;&#x2F;发送请求完成分配角色的操作</span><br><span class="line">      const &#123;data:res&#125; &#x3D; await this.$http.put(&#96;users&#x2F;$&#123;this.userInfo.id&#125;&#x2F;role&#96;,&#123;rid:this.selectedRoleId&#125;)</span><br><span class="line"></span><br><span class="line">      &#x2F;&#x2F;判断如果删除失败，就做提示</span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200)</span><br><span class="line">        return this.$message.error(&#39;分配角色失败&#39;)</span><br><span class="line"></span><br><span class="line">      this.$message.success(&#39;分配角色成功&#39;)</span><br><span class="line">      this.getUserList();</span><br><span class="line">      &#x2F;&#x2F;关闭对话框</span><br><span class="line">      this.setRoleDialogVisible &#x3D; false</span><br><span class="line">    &#125;,</span><br><span class="line">    setRoleDialogClosed()&#123;</span><br><span class="line">      &#x2F;&#x2F;当关闭对话框的时候，重置下拉框中的内容</span><br><span class="line">      this.selectedRoleId &#x3D; &#39;&#39;</span><br><span class="line">      this.userInfo &#x3D; &#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="9-5-将代码推送到码云"><a href="#9-5-将代码推送到码云" class="headerlink" title="9.5 将代码推送到码云"></a>9.5 将代码推送到码云</h2><p>将代码推送到暂存区:git add .</p>
<p>将代码提交到仓库:git commit -m ‘完成了权限功能开发’</p>
<p>将rights分支代码推送到码云:git push</p>
<p>将代码合并到master :git checkout master<br>git merge rights</p>
<p>将master代码推送到码云:git push</p>
<h1 id="10-商品分类"><a href="#10-商品分类" class="headerlink" title="10.商品分类"></a>10.商品分类</h1><h2 id="10-1-新建分支goods-cate"><a href="#10-1-新建分支goods-cate" class="headerlink" title="10.1 新建分支goods_cate"></a>10.1 新建分支goods_cate</h2><p>新建分支goods_cate并推送到码云<br>git checkout -b goods_cate<br>git push -u origin goods_cate</p>
<h2 id="10-2-创建子级路由"><a href="#10-2-创建子级路由" class="headerlink" title="10.2 创建子级路由"></a>10.2 创建子级路由</h2><p>创建categories子级路由组件并设置路由规则</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">import Cate from &#39;.&#x2F;components&#x2F;goods&#x2F;Cate.vue&#39;</span><br><span class="line"></span><br><span class="line">path: &#39;&#x2F;home&#39;, component: Home, redirect: &#39;&#x2F;welcome&#39;, children: [</span><br><span class="line">    &#123; path: &quot;&#x2F;welcome&quot;, component: Welcome &#125;,</span><br><span class="line">    &#123; path: &quot;&#x2F;users&quot;, component: Users &#125;,</span><br><span class="line">    &#123; path: &quot;&#x2F;rights&quot;, component: Rights &#125;,</span><br><span class="line">    &#123; path: &quot;&#x2F;roles&quot;, component: Roles  &#125;,</span><br><span class="line">    &#123; path: &quot;&#x2F;categories&quot;, component: Cate  &#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<h2 id="10-3-添加组件基本布局"><a href="#10-3-添加组件基本布局" class="headerlink" title="10.3 添加组件基本布局"></a>10.3 添加组件基本布局</h2><p>在Cate.vue组件中添加面包屑导航以及卡片视图中的添加分类按钮</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;!-- 面包屑导航 --&gt;</span><br><span class="line">        &lt;el-breadcrumb separator&#x3D;&quot;&#x2F;&quot;&gt;</span><br><span class="line">            &lt;el-breadcrumb-item :to&#x3D;&quot;&#123; path: &#39;&#x2F;home&#39; &#125;&quot;&gt;首页&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">            &lt;el-breadcrumb-item&gt;商品管理&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">            &lt;el-breadcrumb-item&gt;商品分类&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">        &lt;&#x2F;el-breadcrumb&gt;</span><br><span class="line">        &lt;!-- 卡片视图区域 --&gt;</span><br><span class="line">        &lt;el-card&gt;</span><br><span class="line">            &lt;!-- 添加分类按钮区域 --&gt;</span><br><span class="line">            &lt;el-row&gt;</span><br><span class="line">                &lt;el-col&gt;</span><br><span class="line">                    &lt;el-button type&#x3D;&quot;primary&quot;&gt;添加分类&lt;&#x2F;el-button&gt;</span><br><span class="line">                &lt;&#x2F;el-col&gt;</span><br><span class="line">            &lt;&#x2F;el-row&gt;</span><br><span class="line">            &lt;!-- 分类表格  --&gt;</span><br><span class="line"></span><br><span class="line">            &lt;!-- 分页 --&gt;</span><br><span class="line">        &lt;&#x2F;el-card&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br></pre></td></tr></table></figure>

<h2 id="10-4-请求分类数据"><a href="#10-4-请求分类数据" class="headerlink" title="10.4 请求分类数据"></a>10.4 请求分类数据</h2><p>请求分类数据并将数据保存在data中</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      &#x2F;&#x2F; 商品分类数据列表</span><br><span class="line">      cateList: [],</span><br><span class="line">      &#x2F;&#x2F; 查询分类数据的条件</span><br><span class="line">      queryInfo: &#123;</span><br><span class="line">        type: 3,</span><br><span class="line">        pagenum: 1,</span><br><span class="line">        pagesize: 5</span><br><span class="line">      &#125;,</span><br><span class="line">      &#x2F;&#x2F; 保存总数据条数</span><br><span class="line">      total: 0</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  created() &#123;</span><br><span class="line">    this.getCateList()</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    async getCateList() &#123;</span><br><span class="line">      &#x2F;&#x2F; 获取商品分类数据</span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.get(&#39;categories&#39;, &#123;</span><br><span class="line">        params: queryInfo</span><br><span class="line">      &#125;)</span><br><span class="line"></span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">        return this.$message.error(&#39;获取商品列表数据失败&#39;)</span><br><span class="line">      &#125;</span><br><span class="line">      &#x2F;&#x2F; 将数据列表赋值给cateList</span><br><span class="line">      this.cateList &#x3D; res.data.result</span><br><span class="line">      &#x2F;&#x2F; 保存总数据条数</span><br><span class="line">      this.total &#x3D; res.data.total</span><br><span class="line">      &#x2F;&#x2F; console.log(res.data);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<h2 id="10-5-使用插件展示数据"><a href="#10-5-使用插件展示数据" class="headerlink" title="10.5 使用插件展示数据"></a>10.5 使用插件展示数据</h2><p>使用第三方插件vue-table-with-tree-grid展示分类数据</p>
<h3 id="10-5-1-在vue-控制台中点击依赖-gt-安装依赖-gt-运行依赖-gt-输入vue-table-with-tree-gird-gt-点击安装"><a href="#10-5-1-在vue-控制台中点击依赖-gt-安装依赖-gt-运行依赖-gt-输入vue-table-with-tree-gird-gt-点击安装" class="headerlink" title="10.5.1 在vue 控制台中点击依赖-&gt;安装依赖-&gt;运行依赖-&gt;输入vue-table-with-tree-gird-&gt;点击安装"></a>10.5.1 在vue 控制台中点击依赖-&gt;安装依赖-&gt;运行依赖-&gt;输入vue-table-with-tree-gird-&gt;点击安装</h3><h3 id="10-5-2-打开main-js，导入vue-table-with-tree-grid"><a href="#10-5-2-打开main-js，导入vue-table-with-tree-grid" class="headerlink" title="10.5.2 打开main.js，导入vue-table-with-tree-grid"></a>10.5.2 打开main.js，导入vue-table-with-tree-grid</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> TreeTable <span class="keyword">from</span> <span class="string">&#x27;vue-table-with-tree-grid&#x27;</span></span><br><span class="line">    .....</span><br><span class="line">    Vue.config.productionTip = <span class="literal">false</span></span><br><span class="line"><span class="comment">// 全局注册组件</span></span><br><span class="line">Vue.component(<span class="string">&#x27;tree-table&#x27;</span>, TreeTable)</span><br></pre></td></tr></table></figure>

<h3 id="10-5-3-使用组件展示分类数据"><a href="#10-5-3-使用组件展示分类数据" class="headerlink" title="10.5.3 使用组件展示分类数据"></a>10.5.3 使用组件展示分类数据</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 分类表格</span><br><span class="line">:data(设置数据源) :columns(设置表格中列配置信息) :selection-type(是否有复选框)</span><br><span class="line">:expand-type(是否展开数据) show-index(是否设置索引列) index-text(设置索引列头)</span><br><span class="line">border(是否添加纵向边框) :show-row-hover(是否鼠标悬停高亮) --&gt;</span><br><span class="line">&lt;tree-table :data&#x3D;&quot;cateList&quot; :columns&#x3D;&quot;columns&quot; :selection-type&#x3D;&quot;false&quot;</span><br><span class="line">:expand-type&#x3D;&quot;false&quot; show-index index-text&#x3D;&quot;#&quot; border :show-row-hover&#x3D;&quot;false&quot;&gt;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;tree-table&gt;</span><br><span class="line"></span><br><span class="line">在数据中添加columns:</span><br><span class="line">columns: [</span><br><span class="line">    &#123;label:&#39;分类名称&#39;,prop:&#39;cat_name&#39;&#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<h2 id="10-6-自定义数据列"><a href="#10-6-自定义数据列" class="headerlink" title="10.6 自定义数据列"></a>10.6 自定义数据列</h2><p>使用vue-table-with-tree-grid定义模板列并添加自定义列</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 先在columns中添加一个列</span><br><span class="line">columns: [</span><br><span class="line">    &#123;label:&#39;分类名称&#39;,prop:&#39;cat_name&#39;&#125;,</span><br><span class="line">    &#x2F;&#x2F; type:&#39;template&#39;(将该列设置为模板列)，template:&#39;isok&#39;(设置该列模板的名称为isok)</span><br><span class="line">    &#123;label:&#39;是否有效&#39;,prop:&#39;&#39;,type:&#39;template&#39;,template:&#39;isok&#39;&#125;,</span><br><span class="line">    &#123;label:&#39;排序&#39;,prop:&#39;&#39;,type:&#39;template&#39;,template:&#39;order&#39;&#125;,</span><br><span class="line">    &#123;label:&#39;操作&#39;,prop:&#39;&#39;,type:&#39;template&#39;,template:&#39;opt&#39;&#125;</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">&lt;!-- 是否有效区域， 设置对应的模板列： slot&#x3D;&quot;isok&quot;(与columns中设置的template一致) --&gt;</span><br><span class="line">&lt;template slot&#x3D;&quot;isok&quot; slot-scope&#x3D;&quot;scope&quot;&gt;</span><br><span class="line">  &lt;i class&#x3D;&quot;el-icon-success&quot; v-if&#x3D;&quot;scope.row.cat_deleted &#x3D;&#x3D;&#x3D; false&quot; style&#x3D;&quot;color:lightgreen&quot;&gt;&lt;&#x2F;i&gt;</span><br><span class="line">  &lt;i class&#x3D;&quot;el-icon-error&quot; v-else style&#x3D;&quot;color:red&quot;&gt;&lt;&#x2F;i&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line">&lt;!-- 排序 --&gt;</span><br><span class="line">&lt;template slot&#x3D;&quot;order&quot; slot-scope&#x3D;&quot;scope&quot;&gt;</span><br><span class="line">  &lt;el-tag size&#x3D;&quot;mini&quot; v-if&#x3D;&quot;scope.row.cat_level&#x3D;&#x3D;&#x3D;0&quot;&gt;一级&lt;&#x2F;el-tag&gt;</span><br><span class="line">  &lt;el-tag size&#x3D;&quot;mini&quot; type&#x3D;&quot;success&quot; v-else-if&#x3D;&quot;scope.row.cat_level&#x3D;&#x3D;&#x3D;1&quot;&gt;二级&lt;&#x2F;el-tag&gt;</span><br><span class="line">  &lt;el-tag size&#x3D;&quot;mini&quot; type&#x3D;&quot;warning&quot; v-else&gt;三级&lt;&#x2F;el-tag&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;!-- 操作 --&gt;</span><br><span class="line">&lt;template slot&#x3D;&quot;opt&quot; slot-scope&#x3D;&quot;scope&quot;&gt;</span><br><span class="line">  &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;primary&quot; icon&#x3D;&quot;el-icon-edit&quot;&gt;编辑&lt;&#x2F;el-button&gt;</span><br><span class="line">  &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;danger&quot; icon&#x3D;&quot;el-icon-delete&quot;&gt;删除&lt;&#x2F;el-button&gt; </span><br><span class="line">&lt;&#x2F;template&gt;</span><br></pre></td></tr></table></figure>

<h2 id="10-7-完成分页功能"><a href="#10-7-完成分页功能" class="headerlink" title="10.7 完成分页功能"></a>10.7 完成分页功能</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 分页 --&gt;</span><br><span class="line">&lt;el-pagination @size-change&#x3D;&quot;handleSizeChange&quot; @current-change&#x3D;&quot;handleCurrentChange&quot; :current-page&#x3D;&quot;queryInfo.pagenum&quot; :page-sizes&#x3D;&quot;[3, 5, 10, 15]&quot; :page-size&#x3D;&quot;queryInfo.pagesize&quot; layout&#x3D;&quot;total, sizes, prev, pager, next, jumper&quot; :total&#x3D;&quot;total&quot;&gt;</span><br><span class="line">&lt;&#x2F;el-pagination&gt;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 添加对应的事件函数</span><br><span class="line">methods:&#123;</span><br><span class="line">  .......</span><br><span class="line">  handleSizeChange(newSize)&#123;</span><br><span class="line">    &#x2F;&#x2F; 当pagesize发生改变时触发</span><br><span class="line">    this.queryInfo.pagesize &#x3D; newSize;</span><br><span class="line">    this.getCateList();</span><br><span class="line">  &#125;,</span><br><span class="line">  handleCurrentChange(newPage)&#123;</span><br><span class="line">    &#x2F;&#x2F; 当pagenum发生改变时触发</span><br><span class="line">    this.queryInfo.pagenum &#x3D; newPage;</span><br><span class="line">    this.getCateList();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="10-8-完成添加分类"><a href="#10-8-完成添加分类" class="headerlink" title="10.8 完成添加分类"></a>10.8 完成添加分类</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line">......</span><br><span class="line">&lt;!-- 添加分类按钮区域 --&gt;</span><br><span class="line">&lt;el-row&gt;</span><br><span class="line">  &lt;el-col&gt;</span><br><span class="line">    &lt;el-button type&#x3D;&quot;primary&quot; @click&#x3D;&quot;showAddCateDialog&quot;&gt;添加分类&lt;&#x2F;el-button&gt;</span><br><span class="line">  &lt;&#x2F;el-col&gt;</span><br><span class="line">&lt;&#x2F;el-row&gt;</span><br><span class="line">......</span><br><span class="line">&lt;!-- 添加分类对话框 --&gt;</span><br><span class="line">&lt;el-dialog title&#x3D;&quot;添加分类&quot; :visible.sync&#x3D;&quot;addCateDialogVisible&quot; width&#x3D;&quot;50%&quot;  @close&#x3D;&quot;addCateDialogClosed&quot;&gt;</span><br><span class="line">  &lt;!-- 添加分类表单 --&gt;</span><br><span class="line">  &lt;el-form :model&#x3D;&quot;addCateForm&quot; :rules&#x3D;&quot;addCateFormRules&quot; ref&#x3D;&quot;addCateFormRuleForm&quot; label-width&#x3D;&quot;100px&quot;&gt;</span><br><span class="line">    &lt;el-form-item label&#x3D;&quot;分类名称&quot; prop&#x3D;&quot;cat_name&quot;&gt;</span><br><span class="line">      &lt;el-input v-model&#x3D;&quot;addCateForm.cat_name&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">    &lt;&#x2F;el-form-item&gt;</span><br><span class="line">    &lt;el-form-item label&#x3D;&quot;父级分类&quot; prop&#x3D;&quot;cat_pid&quot;&gt;</span><br><span class="line">      </span><br><span class="line">    &lt;&#x2F;el-form-item&gt;</span><br><span class="line">  &lt;&#x2F;el-form&gt;</span><br><span class="line">  &lt;span slot&#x3D;&quot;footer&quot; class&#x3D;&quot;dialog-footer&quot;&gt;</span><br><span class="line">    &lt;el-button @click&#x3D;&quot;addCateDialogVisible &#x3D; false&quot;&gt;取 消&lt;&#x2F;el-button&gt;</span><br><span class="line">    &lt;el-button type&#x3D;&quot;primary&quot; @click&#x3D;&quot;addCate&quot;&gt;确 定&lt;&#x2F;el-button&gt;</span><br><span class="line">  &lt;&#x2F;span&gt;</span><br><span class="line">&lt;&#x2F;el-dialog&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 用来显示或隐藏添加分类对话框</span><br><span class="line">addCateDialogVisible: false,</span><br><span class="line">&#x2F;&#x2F; 添加分类的表单数据对象</span><br><span class="line">addCateForm:&#123;</span><br><span class="line">  &#x2F;&#x2F; 分类名称</span><br><span class="line">  cat_name:&#39;&#39;,</span><br><span class="line">  &#x2F;&#x2F; 添加分类的父级id，0则表示父级为0.添加一级分类</span><br><span class="line">  cat_pid:0,</span><br><span class="line">  &#x2F;&#x2F; 添加分类的等级，0则表示添加一级分类</span><br><span class="line">  cat_level:0</span><br><span class="line">&#125;,</span><br><span class="line">&#x2F;&#x2F; 添加分类校验规则</span><br><span class="line">addCateFormRules:&#123;</span><br><span class="line">  &#x2F;&#x2F; 验证规则</span><br><span class="line">  cat_name:[ &#123;required:true , message:&#39;请输入分类名称&#39;,trigger:&#39;blur&#39;&#125; ]</span><br><span class="line">&#125;,</span><br><span class="line">&#x2F;&#x2F; 保存1,2级父级分类的列表</span><br><span class="line">parentCateList:[]</span><br><span class="line">.......</span><br><span class="line">showAddCateDialog() &#123;</span><br><span class="line">  &#x2F;&#x2F; 调用getParentCateList获取分类列表</span><br><span class="line">  this.getParentCateList()</span><br><span class="line">  &#x2F;&#x2F; 显示添加分类对话框</span><br><span class="line">  this.addCateDialogVisible &#x3D; true</span><br><span class="line">&#125;,</span><br><span class="line">async getParentCateList()&#123;</span><br><span class="line">  &#x2F;&#x2F; 获取父级分类数据列表</span><br><span class="line">  const &#123; data: res &#125; &#x3D; await this.$http.get(&#39;categories&#39;, &#123;</span><br><span class="line">    params: &#123;type:2&#125;</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">    return this.$message.error(&#39;获取商品分类列表数据失败&#39;)</span><br><span class="line">  &#125;</span><br><span class="line">  this.parentCateList &#x3D; res.data</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>添加级联菜单显示父级分类<br>先导入Cascader组件，并注册<br>然后添加使用级联菜单组件：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">&lt;el-form-item label&#x3D;&quot;父级分类&quot; prop&#x3D;&quot;cat_pid&quot;&gt;</span><br><span class="line">  &lt;!-- expandTrigger&#x3D;&#39;hover&#39;(鼠标悬停触发级联) v-model(设置级联菜单绑定数据) :options(指定级联菜单数据源)  :props(用来配置数据显示的规则) </span><br><span class="line">  clearable(提供“X”号完成删除文本功能) change-on-select(是否可以选中任意一级的菜单) --&gt;</span><br><span class="line">  &lt;el-cascader expandTrigger&#x3D;&#39;hover&#39; v-model&#x3D;&quot;selectedKeys&quot; :options&#x3D;&quot;parentCateList&quot; :props&#x3D;&quot;cascaderProps&quot; @change&#x3D;&quot;parentCateChange&quot; clearable change-on-select&gt;&lt;&#x2F;el-cascader&gt;</span><br><span class="line">&lt;&#x2F;el-form-item&gt;</span><br><span class="line"></span><br><span class="line">添加数据</span><br><span class="line">&#x2F;&#x2F; 配置级联菜单中数据如何展示</span><br><span class="line">cascaderProps:&#123;</span><br><span class="line">  value:&#39;cat_id&#39;,</span><br><span class="line">  label:&#39;cat_name&#39;,</span><br><span class="line">  children:&#39;children&#39;,</span><br><span class="line">  expandTrigger:&#39;hover&#39;</span><br><span class="line">&#125;,</span><br><span class="line">&#x2F;&#x2F;绑定用户选择的分类值</span><br><span class="line">selectedKeys:[]</span><br><span class="line">.....</span><br><span class="line">methods:&#123;</span><br><span class="line">  .....</span><br><span class="line">  parentCateChange()&#123;</span><br><span class="line">    &#x2F;&#x2F; 级联菜单中选择项发生变化时触发</span><br><span class="line">    console.log(this.selectedKeys)</span><br><span class="line">    &#x2F;&#x2F; 如果用户选择了父级分类</span><br><span class="line">    if (this.selectedKeys.length &gt; 0) &#123;</span><br><span class="line">      &#x2F;&#x2F; 则将数组中的最后一项设置为父级分类</span><br><span class="line">      this.addCateForm.cat_pid &#x3D; this.selectedKeys[this.selectedKeys.length - 1]</span><br><span class="line">      &#x2F;&#x2F; level也要跟着发生变化</span><br><span class="line">      this.addCateForm.cat_level &#x3D; this.selectedKeys.length</span><br><span class="line">      return</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">      this.addCateForm.cat_pid &#x3D; 0</span><br><span class="line">      this.addCateForm.cat_level &#x3D; 0</span><br><span class="line">      return</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  addCateDialogClosed() &#123;</span><br><span class="line">    &#x2F;&#x2F;当关闭添加分类对话框时，重置表单</span><br><span class="line">    this.$refs.addCateFormRef.resetFields()</span><br><span class="line">    this.selectedKeys &#x3D; [];</span><br><span class="line">    this.addCateForm.cat_pid &#x3D; 0</span><br><span class="line">    this.addCateForm.cat_level &#x3D; 0</span><br><span class="line">  &#125;,</span><br><span class="line">  addCate() &#123;</span><br><span class="line">    &#x2F;&#x2F;点击确定，完成添加分类</span><br><span class="line">    console.log(this.addCateForm)</span><br><span class="line">    this.$refs.addCateFormRef.validate(async valid &#x3D;&gt; &#123;</span><br><span class="line">      if (!valid) return</span><br><span class="line">      &#x2F;&#x2F;发送请求完成添加分类</span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.post(</span><br><span class="line">        &#39;categories&#39;,</span><br><span class="line">        this.addCateForm</span><br><span class="line">      )</span><br><span class="line"></span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 201) &#123;</span><br><span class="line">        return this.$message.error(&#39;添加分类失败&#39;)</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      this.$message.success(&#39;添加分类成功&#39;)</span><br><span class="line">      this.getCateList()</span><br><span class="line">      this.addCateDialogVisible &#x3D; false</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="10-9-完成编辑和删除功能"><a href="#10-9-完成编辑和删除功能" class="headerlink" title="10.9 完成编辑和删除功能"></a>10.9 完成编辑和删除功能</h2><p>在编辑和删除按钮上增加功能</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 操作 --&gt;</span><br><span class="line">      &lt;template slot&#x3D;&quot;opt&quot; slot-scope&#x3D;&quot;scope&quot;&gt;</span><br><span class="line">        &lt;el-button type&#x3D;&quot;primary&quot; icon&#x3D;&quot;el-icon-edit&quot; size&#x3D;&quot;mini&quot; @click&#x3D;&quot;showEditCateDialog(scope.row.cat_id)&quot;&gt;编辑&lt;&#x2F;el-button&gt;</span><br><span class="line">        &lt;el-button type&#x3D;&quot;danger&quot; icon&#x3D;&quot;el-icon-delete&quot; size&#x3D;&quot;mini&quot; @click&#x3D;&quot;removeCateById(scope.row.cat_id)&quot;&gt;删除&lt;&#x2F;el-button&gt;</span><br><span class="line">      &lt;&#x2F;template&gt;</span><br></pre></td></tr></table></figure>

<p>完成编辑按钮的对话框</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 编辑分类的对话框 --&gt;</span><br><span class="line">   &lt;el-dialog title&#x3D;&quot;编辑分类&quot; :visible.sync&#x3D;&quot;editCateDialogVisible&quot; width&#x3D;&quot;50%&quot; @close&#x3D;&quot;editCateDialogClosed&quot;&gt;</span><br><span class="line">     &lt;!-- 编辑分类的表单 --&gt;</span><br><span class="line">     &lt;el-form :model&#x3D;&quot;editCateForm&quot; :rules&#x3D;&quot;editCateFormRules&quot; ref&#x3D;&quot;editCateFormRef&quot; label-width&#x3D;&quot;100px&quot;&gt;</span><br><span class="line">       &lt;el-form-item label&#x3D;&quot;分类名称：&quot; prop&#x3D;&quot;cat_name&quot;&gt;</span><br><span class="line">         &lt;el-input v-model&#x3D;&quot;editCateForm.cat_name&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">       &lt;&#x2F;el-form-item&gt;</span><br><span class="line">     &lt;&#x2F;el-form&gt;</span><br><span class="line">     &lt;span slot&#x3D;&quot;footer&quot; class&#x3D;&quot;dialog-footer&quot;&gt;</span><br><span class="line">       &lt;el-button @click&#x3D;&quot;editCateDialogVisible &#x3D; false&quot;&gt;取 消&lt;&#x2F;el-button&gt;</span><br><span class="line">       &lt;el-button type&#x3D;&quot;primary&quot; @click&#x3D;&quot;editCate&quot;&gt;确 定&lt;&#x2F;el-button&gt;</span><br><span class="line">     &lt;&#x2F;span&gt;</span><br><span class="line">   &lt;&#x2F;el-dialog&gt;</span><br></pre></td></tr></table></figure>

<p>在script完成对应的参数和函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br></pre></td><td class="code"><pre><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      editCateDialogVisible: false,</span><br><span class="line">      &#x2F;&#x2F; 添加分类的表单数据对象</span><br><span class="line">      editCateForm: &#123;</span><br><span class="line">        &#x2F;&#x2F; 将要修改的分类id</span><br><span class="line">        cat_id: 0,</span><br><span class="line">        &#x2F;&#x2F; 将要修改的分类名称</span><br><span class="line">        cat_name: &#39;&#39;,</span><br><span class="line">        &#x2F;&#x2F; 父级分类的Id</span><br><span class="line">        cat_pid: 0,</span><br><span class="line">        &#x2F;&#x2F; 分类的等级，默认要修改的是1级分类</span><br><span class="line">        cat_level: 0</span><br><span class="line">      &#125;,</span><br><span class="line">      &#x2F;&#x2F; 添加分类表单的验证规则对象</span><br><span class="line">      editCateFormRules: &#123;</span><br><span class="line">        cat_name: [&#123; required: true, message: &#39;请输入分类名称&#39;, trigger: &#39;blur&#39; &#125;]</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    &#x2F;&#x2F; 根据Id删除对应的用户信息</span><br><span class="line">    async removeCateById(catId) &#123;</span><br><span class="line">      &#x2F;&#x2F; 弹框询问用户是否删除数据</span><br><span class="line">      const confirmResult &#x3D; await this.$confirm(&#39;此操作将永久删除该商品, 是否继续?&#39;, &#39;提示&#39;, &#123;</span><br><span class="line">        confirmButtonText: &#39;确定&#39;,</span><br><span class="line">        cancelButtonText: &#39;取消&#39;,</span><br><span class="line">        type: &#39;warning&#39;</span><br><span class="line">      &#125;).catch(err &#x3D;&gt; err)</span><br><span class="line"></span><br><span class="line">      &#x2F;&#x2F; 如果用户确认删除，则返回值为字符串 confirm</span><br><span class="line">      &#x2F;&#x2F; 如果用户取消了删除，则返回值为字符串 cancel</span><br><span class="line">      &#x2F;&#x2F; console.log(confirmResult)</span><br><span class="line">      if (confirmResult !&#x3D;&#x3D; &#39;confirm&#39;) &#123;</span><br><span class="line">        return this.$message.info(&#39;已取消删除&#39;)</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.delete(&#39;categories&#x2F;&#39; + catId)</span><br><span class="line"></span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">        return this.$message.error(&#39;删除商品失败！&#39;)</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      this.$message.success(&#39;删除商品成功！&#39;)</span><br><span class="line">      this.getCateList()</span><br><span class="line">    &#125;,</span><br><span class="line">    &#x2F;&#x2F; 展示编辑商品的对话框</span><br><span class="line">    async showEditCateDialog(id) &#123;</span><br><span class="line">      &#x2F;&#x2F; 先获取父级分类的数据列表</span><br><span class="line">      this.getParentCateList()</span><br><span class="line">      &#x2F;&#x2F; console.log(id)</span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.get(&#39;categories&#x2F;&#39; + id)</span><br><span class="line"></span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">        return this.$message.error(&#39;查询商品信息失败！&#39;)</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      this.editCateForm &#x3D; res.data</span><br><span class="line">      this.editCateDialogVisible &#x3D; true</span><br><span class="line">    &#125;,</span><br><span class="line">    &#x2F;&#x2F; 点击按钮，编辑分类</span><br><span class="line">    editCate() &#123;</span><br><span class="line">      this.$refs.editCateFormRef.validate(async valid &#x3D;&gt; &#123;</span><br><span class="line">        if (!valid) return</span><br><span class="line">        const &#123; data: res &#125; &#x3D; await this.$http.put(&#39;categories&#x2F;&#39; + this.editCateForm.cat_id, this.editCateForm)</span><br><span class="line"></span><br><span class="line">        if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">          return this.$message.error(&#39;编辑分类失败!&#39;)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        this.$message.success(&#39;编辑分类成功!&#39;)</span><br><span class="line">        this.getCateList()</span><br><span class="line">        this.editCateDialogVisible &#x3D; false</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;,</span><br><span class="line">    &#x2F;&#x2F; 监听对话框的关闭事件，重置表单数据</span><br><span class="line">    editCateDialogClosed() &#123;</span><br><span class="line">      this.$refs.editCateFormRef.resetFields()</span><br><span class="line">      this.selectedKeys &#x3D; []</span><br><span class="line">      this.editCateForm.cat_level &#x3D; 0</span><br><span class="line">      this.editCateForm.cat_pid &#x3D; 0</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<h2 id="10-10-推送代码"><a href="#10-10-推送代码" class="headerlink" title="10.10 推送代码"></a>10.10 推送代码</h2><p>制作完添加分类之后，将代码提交到仓库，推送到码云,将goods_cate分支合并到master<br>git add .<br>git commit -m ‘完成商品分类’<br>git push<br>git checkout master<br>git merge goods_cate</p>
<h1 id="11-参数管理"><a href="#11-参数管理" class="headerlink" title="11.参数管理"></a>11.参数管理</h1><h2 id="11-1-新建分支goods-params"><a href="#11-1-新建分支goods-params" class="headerlink" title="11.1 新建分支goods_params"></a>11.1 新建分支goods_params</h2><p>只允许给三级分类内容设置参数，参数分为动态参数和静态参数属性</p>
<p>新建分支 goods_params并推送到码云<br>git checkout -b  goods_params<br>git push -u origin  goods_params</p>
<h2 id="11-2-添加子级组件"><a href="#11-2-添加子级组件" class="headerlink" title="11.2 添加子级组件"></a>11.2 添加子级组件</h2><p>添加Params.vue子组件，并在router.js中引入该组件并设置路由规则</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">import Params from &#39;.&#x2F;components&#x2F;goods&#x2F;Params.vue&#39;</span><br><span class="line">......</span><br><span class="line">path: &#39;&#x2F;home&#39;, component: Home, redirect: &#39;&#x2F;welcome&#39;, children: [</span><br><span class="line">  &#123; path: &quot;&#x2F;welcome&quot;, component: Welcome &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;users&quot;, component: Users &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;rights&quot;, component: Rights &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;roles&quot;, component: Roles  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;categories&quot;, component: Cate  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;params&quot;, component: Params  &#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<h2 id="11-3-完成组件基本布局"><a href="#11-3-完成组件基本布局" class="headerlink" title="11.3 完成组件基本布局"></a>11.3 完成组件基本布局</h2><p>完成Params.vue组件的基本布局<br>其中警告提示信息使用了el-alert，在element.js引入该组件并注册</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;h3&gt;分类参数&lt;&#x2F;h3&gt;</span><br><span class="line">        &lt;!-- 面包屑导航 --&gt;</span><br><span class="line">        &lt;el-breadcrumb separator&#x3D;&quot;&#x2F;&quot;&gt;</span><br><span class="line">            &lt;el-breadcrumb-item :to&#x3D;&quot;&#123; path: &#39;&#x2F;home&#39; &#125;&quot;&gt;首页&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">            &lt;el-breadcrumb-item&gt;商品管理&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">            &lt;el-breadcrumb-item&gt;分类参数&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">        &lt;&#x2F;el-breadcrumb&gt;</span><br><span class="line">        &lt;!-- 卡片视图区域 --&gt;</span><br><span class="line">        &lt;el-card&gt;</span><br><span class="line">            &lt;!-- 警告区域 :closable&#x3D;&quot;false&quot;(是否展示“X”号) show-icon(显示图标) --&gt;</span><br><span class="line">            &lt;el-alert title&#x3D;&quot;注意：只允许为第三级分类设置相关参数&quot; type&#x3D;&quot;warning&quot; :closable&#x3D;&quot;false&quot; show-icon&gt;</span><br><span class="line">            &lt;&#x2F;el-alert&gt;</span><br><span class="line"></span><br><span class="line">            &lt;!-- 选择商品分类区域 --&gt;</span><br><span class="line">            &lt;el-row class&#x3D;&quot;cat_opt&quot;&gt;</span><br><span class="line">                &lt;el-col&gt;</span><br><span class="line">                    &lt;span&gt;选择商品分类：&lt;&#x2F;span&gt;</span><br><span class="line">                    &lt;!-- 选择商品分类的级联选择框 --&gt;</span><br><span class="line">                &lt;&#x2F;el-col&gt;</span><br><span class="line">                &lt;el-col&gt;&lt;&#x2F;el-col&gt;</span><br><span class="line">            &lt;&#x2F;el-row&gt;</span><br><span class="line">        &lt;&#x2F;el-card&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br></pre></td></tr></table></figure>

<h2 id="11-4-完成级联选择框"><a href="#11-4-完成级联选择框" class="headerlink" title="11.4 完成级联选择框"></a>11.4 完成级联选择框</h2><p>完成商品分类级联选择框</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 选择商品分类区域 --&gt;</span><br><span class="line">&lt;el-row class&#x3D;&quot;cat_opt&quot;&gt;</span><br><span class="line">    &lt;el-col&gt;</span><br><span class="line">        &lt;span&gt;选择商品分类：&lt;&#x2F;span&gt;</span><br><span class="line">        &lt;!-- 选择商品分类的级联选择框 --&gt;</span><br><span class="line">        &lt;el-cascader expandTrigger&#x3D;&#39;hover&#39; v-model&#x3D;&quot;selectedCateKeys&quot; :options&#x3D;&quot;cateList&quot; :props&#x3D;&quot;cateProps&quot; @change&#x3D;&quot;handleChange&quot; clearable&gt;&lt;&#x2F;el-cascader&gt;</span><br><span class="line">    &lt;&#x2F;el-col&gt;</span><br><span class="line">    &lt;el-col&gt;&lt;&#x2F;el-col&gt;</span><br><span class="line">&lt;&#x2F;el-row&gt;</span><br><span class="line">......</span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">        &#x2F;&#x2F; 分类列表</span><br><span class="line">        cateList:[],</span><br><span class="line">        &#x2F;&#x2F; 用户在级联下拉菜单中选中的分类id</span><br><span class="line">        selectedCateKeys:[],</span><br><span class="line">        &#x2F;&#x2F; 配置级联菜单中数据如何展示</span><br><span class="line">        cateProps: &#123;</span><br><span class="line">            value: &#39;cat_id&#39;,</span><br><span class="line">            label: &#39;cat_name&#39;,</span><br><span class="line">            children: &#39;children&#39;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  created() &#123;</span><br><span class="line">      this.getCateList()</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">      async getCateList()&#123;</span><br><span class="line">        &#x2F;&#x2F; 获取所有的商品分类列表</span><br><span class="line">        const &#123; data: res &#125; &#x3D; await this.$http.get(&#39;categories&#39;)</span><br><span class="line"></span><br><span class="line">        if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">            return this.$message.error(&#39;获取分类数据失败&#39;)</span><br><span class="line">        &#125;</span><br><span class="line">        &#x2F;&#x2F; 将数据列表赋值给cateList</span><br><span class="line">        this.cateList &#x3D; res.data</span><br><span class="line">        &#x2F;&#x2F; 保存总数据条数</span><br><span class="line">        &#x2F;&#x2F; this.total &#x3D; res.data.total</span><br><span class="line">        &#x2F;&#x2F; console.log(res.data);</span><br><span class="line">      &#125;,</span><br><span class="line">      handleChange()&#123;</span><br><span class="line">        &#x2F;&#x2F; 当用户在级联菜单中选择内容改变时触发</span><br><span class="line">        console.log(this.selectedCateKeys);</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<h2 id="11-5-展示参数"><a href="#11-5-展示参数" class="headerlink" title="11.5 展示参数"></a>11.5 展示参数</h2><p>展示动态参数数据以及静态属性数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- tab页签区域 --&gt;</span><br><span class="line">&lt;el-tabs v-model&#x3D;&quot;activeName&quot; @tab-click&#x3D;&quot;handleTabClick&quot;&gt;</span><br><span class="line">  &lt;!-- 添加动态参数的面板 将标签页改为many --&gt;</span><br><span class="line">  &lt;el-tab-pane label&#x3D;&quot;动态参数&quot; name&#x3D;&quot;many&quot;&gt;</span><br><span class="line">    &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;primary&quot; :disabled&#x3D;&quot;isButtonDisabled&quot;&gt;添加参数&lt;&#x2F;el-button&gt;</span><br><span class="line">    &lt;!-- 动态参数表格 --&gt;</span><br><span class="line">    &lt;el-table :data&#x3D;&quot;manyTableData&quot; border stripe&gt;</span><br><span class="line">      &lt;!-- 展开行 --&gt;</span><br><span class="line">      &lt;el-table-column type&#x3D;&quot;expand&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">      &lt;!-- 索引列 --&gt;</span><br><span class="line">      &lt;el-table-column type&#x3D;&quot;index&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">      &lt;el-table-column label&#x3D;&quot;参数名称&quot; prop&#x3D;&quot;attr_name&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">      &lt;el-table-column label&#x3D;&quot;操作&quot;&gt;</span><br><span class="line">        &lt;template slot-scope&#x3D;&quot;scope&quot;&gt;</span><br><span class="line">          &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;primary&quot; icon&#x3D;&quot;el-icon-edit&quot;&gt;编辑&lt;&#x2F;el-button&gt;</span><br><span class="line">          &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;danger&quot; icon&#x3D;&quot;el-icon-delete&quot;&gt;删除&lt;&#x2F;el-button&gt;</span><br><span class="line">        &lt;&#x2F;template&gt;</span><br><span class="line">      &lt;&#x2F;el-table-column&gt;</span><br><span class="line">    &lt;&#x2F;el-table&gt;</span><br><span class="line">  &lt;&#x2F;el-tab-pane&gt;</span><br><span class="line">  &lt;!-- 添加静态属性的面板 将标签页改为only --&gt;</span><br><span class="line">  &lt;el-tab-pane label&#x3D;&quot;静态属性&quot; name&#x3D;&quot;only&quot;&gt;</span><br><span class="line">    &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;primary&quot; :disabled&#x3D;&quot;isButtonDisabled&quot;&gt;添加属性&lt;&#x2F;el-button&gt;</span><br><span class="line">    &lt;!-- 静态属性表格 --&gt;</span><br><span class="line">    &lt;el-table :data&#x3D;&quot;onlyTableData&quot; border stripe&gt;</span><br><span class="line">      &lt;!-- 展开行 --&gt;</span><br><span class="line">      &lt;el-table-column type&#x3D;&quot;expand&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">      &lt;!-- 索引列 --&gt;</span><br><span class="line">      &lt;el-table-column type&#x3D;&quot;index&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">      &lt;el-table-column label&#x3D;&quot;属性名称&quot; prop&#x3D;&quot;attr_name&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">      &lt;el-table-column label&#x3D;&quot;操作&quot;&gt;</span><br><span class="line">        &lt;template slot-scope&#x3D;&quot;scope&quot;&gt;</span><br><span class="line">          &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;primary&quot; icon&#x3D;&quot;el-icon-edit&quot;&gt;编辑&lt;&#x2F;el-button&gt;</span><br><span class="line">          &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;danger&quot; icon&#x3D;&quot;el-icon-delete&quot;&gt;删除&lt;&#x2F;el-button&gt;</span><br><span class="line">        &lt;&#x2F;template&gt;</span><br><span class="line">      &lt;&#x2F;el-table-column&gt;</span><br><span class="line">    &lt;&#x2F;el-table&gt;</span><br><span class="line">  &lt;&#x2F;el-tab-pane&gt;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;el-tabs&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      ......</span><br><span class="line">      &#x2F;&#x2F;tab页签激活显示的页签项</span><br><span class="line">      activeName: &#39;many&#39;,</span><br><span class="line">      &#x2F;&#x2F;用来保存动态参数数据</span><br><span class="line">      manyTableData: [],</span><br><span class="line">      &#x2F;&#x2F;用来保存静态属性数据</span><br><span class="line">      onlyTableData: []  </span><br><span class="line">    &#125;</span><br><span class="line">  methods: &#123;</span><br><span class="line">    .......</span><br><span class="line">    async handleChange() &#123;</span><br><span class="line">      &#x2F;&#x2F;当用户在级联菜单中选择内容改变时触发</span><br><span class="line">      console.log(this.selectedCateKeys)</span><br><span class="line">      &#x2F;&#x2F;发送请求，根据用户选择的三级分类和面板获取参数数据</span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.get(</span><br><span class="line">        &#96;categories&#x2F;$&#123;this.cateId&#125;&#x2F;attributes&#96;,</span><br><span class="line">        &#123; params: &#123; sel: this.activeName &#125; &#125;</span><br><span class="line">      )</span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">        return this.$message.error(&#39;获取参数列表数据失败&#39;)</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      console.log(res.data)</span><br><span class="line">      if (this.activeName &#x3D;&#x3D;&#x3D; &#39;many&#39;) &#123;</span><br><span class="line">        &#x2F;&#x2F;获取的是动态参数</span><br><span class="line">        this.manyTableData &#x3D; res.data</span><br><span class="line">      &#125; else if (this.activeName &#x3D;&#x3D;&#x3D; &#39;only&#39;) &#123;</span><br><span class="line">        &#x2F;&#x2F;获取的是静态属性</span><br><span class="line">        this.onlyTableData &#x3D; res.data</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    handleTabClick() &#123;</span><br><span class="line">      console.log(this.activeName)</span><br><span class="line">      this.handleChange()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  computed: &#123;</span><br><span class="line">    &#x2F;&#x2F;添加计算属性用来获取按钮禁用与否</span><br><span class="line">    isButtonDisabled() &#123;</span><br><span class="line">      return this.selectedCateKeys.length !&#x3D;&#x3D; 3</span><br><span class="line">    &#125;,</span><br><span class="line">    &#x2F;&#x2F;获取选中的三级分类id</span><br><span class="line">    cateId() &#123;</span><br><span class="line">      if (this.selectedCateKeys.length &#x3D;&#x3D;&#x3D; 3) &#123;</span><br><span class="line">        return this.selectedCateKeys[this.selectedCateKeys.length - 1]</span><br><span class="line">      &#125;</span><br><span class="line">      return null</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<h2 id="11-6-添加参数"><a href="#11-6-添加参数" class="headerlink" title="11.6 添加参数"></a>11.6 添加参数</h2><p>完成添加参数或属性</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 添加参数或属性对话框 --&gt;</span><br><span class="line">&lt;el-dialog :title&#x3D;&quot;&#39;添加&#39;+titleText&quot; :visible.sync&#x3D;&quot;addDialogVisible&quot; width&#x3D;&quot;50%&quot; @close&#x3D;&quot;addDialogClosed&quot;&gt;</span><br><span class="line">  &lt;!-- 添加表单 --&gt;</span><br><span class="line">  &lt;el-form :model&#x3D;&quot;addForm&quot; :rules&#x3D;&quot;addFormRules&quot; ref&#x3D;&quot;addFormRef&quot; label-width&#x3D;&quot;100px&quot;&gt;</span><br><span class="line">    &lt;el-form-item :label&#x3D;&quot;titleText&quot; prop&#x3D;&quot;attr_name&quot;&gt;</span><br><span class="line">      &lt;el-input v-model&#x3D;&quot;addForm.attr_name&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">    &lt;&#x2F;el-form-item&gt;</span><br><span class="line">  &lt;&#x2F;el-form&gt;</span><br><span class="line">  &lt;span slot&#x3D;&quot;footer&quot; class&#x3D;&quot;dialog-footer&quot;&gt;</span><br><span class="line">    &lt;el-button @click&#x3D;&quot;addDialogVisible &#x3D; false&quot;&gt;取 消&lt;&#x2F;el-button&gt;</span><br><span class="line">    &lt;el-button type&#x3D;&quot;primary&quot; @click&#x3D;&quot;addParams&quot;&gt;确 定&lt;&#x2F;el-button&gt;</span><br><span class="line">  &lt;&#x2F;span&gt;</span><br><span class="line">&lt;&#x2F;el-dialog&gt;</span><br><span class="line"></span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      .......</span><br><span class="line">      &#x2F;&#x2F; 控制添加参数.属性对话框的显示或隐藏</span><br><span class="line">      addDialogVisible: false,</span><br><span class="line">      &#x2F;&#x2F; 添加参数的表单数据对象</span><br><span class="line">      addForm: &#123;</span><br><span class="line">        attr_name: &#39;&#39;</span><br><span class="line">      &#125;,</span><br><span class="line">      &#x2F;&#x2F; 添加表单验证规则</span><br><span class="line">      addFormRules: &#123;</span><br><span class="line">        attr_name: [&#123; required: true, message: &#39;请输入名称&#39;, trigger: &#39;blur&#39; &#125;]</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,methods: &#123;</span><br><span class="line">    .......</span><br><span class="line">    addParams() &#123;</span><br><span class="line">      &#x2F;&#x2F; 当用户点击对话框中的确定时，校验表单</span><br><span class="line">      this.$refs.addFormRef.validate(async valid &#x3D;&gt; &#123;</span><br><span class="line">        &#x2F;&#x2F; 校验不通过，return</span><br><span class="line">        if (!valid) return</span><br><span class="line">        &#x2F;&#x2F; 校验通过，发送请求完成添加参数或者属性</span><br><span class="line">        const &#123; data: res &#125; &#x3D; this.$http.post(&#96;categories&#x2F;$&#123;this.cateId&#125;&#x2F;attributes&#96;,</span><br><span class="line">          &#123; </span><br><span class="line">            attr_name: this.addForm.attr_name, </span><br><span class="line">            attr_sel: this.activeName,</span><br><span class="line">            attr_vals: &quot;a,b,c&quot; </span><br><span class="line">          &#125;</span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">        console.log(res)</span><br><span class="line">        if (res.meta.status !&#x3D;&#x3D; 201) &#123;</span><br><span class="line">          return this.$message.error(&#39;添加&#39; + this.titleText + &#39;数据失败&#39;)</span><br><span class="line">        &#125;</span><br><span class="line">        this.$message.success(&#39;添加&#39; + this.titleText + &#39;数据成功&#39;)</span><br><span class="line">        this.addDialogVisible &#x3D; false</span><br><span class="line">        this.getCateList()</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<h2 id="11-7-编辑参数"><a href="#11-7-编辑参数" class="headerlink" title="11.7 编辑参数"></a>11.7 编辑参数</h2><p>完成编辑参数或属性</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 修改参数或属性对话框 --&gt;</span><br><span class="line">&lt;el-dialog :title&#x3D;&quot;&#39;修改&#39;+titleText&quot; :visible.sync&#x3D;&quot;editDialogVisible&quot; width&#x3D;&quot;50%&quot; @close&#x3D;&quot;editDialogClosed&quot;&gt;</span><br><span class="line">  &lt;!-- 添加表单 --&gt;</span><br><span class="line">  &lt;el-form :model&#x3D;&quot;editForm&quot; :rules&#x3D;&quot;editFormRules&quot; ref&#x3D;&quot;editFormRef&quot; label-width&#x3D;&quot;100px&quot;&gt;</span><br><span class="line">    &lt;el-form-item :label&#x3D;&quot;titleText&quot; prop&#x3D;&quot;attr_name&quot;&gt;</span><br><span class="line">      &lt;el-input v-model&#x3D;&quot;editForm.attr_name&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">    &lt;&#x2F;el-form-item&gt;</span><br><span class="line">  &lt;&#x2F;el-form&gt;</span><br><span class="line">  &lt;span slot&#x3D;&quot;footer&quot; class&#x3D;&quot;dialog-footer&quot;&gt;</span><br><span class="line">    &lt;el-button @click&#x3D;&quot;editDialogVisible &#x3D; false&quot;&gt;取 消&lt;&#x2F;el-button&gt;</span><br><span class="line">    &lt;el-button type&#x3D;&quot;primary&quot; @click&#x3D;&quot;editParams&quot;&gt;确 定&lt;&#x2F;el-button&gt;</span><br><span class="line">  &lt;&#x2F;span&gt;</span><br><span class="line">&lt;&#x2F;el-dialog&gt;</span><br><span class="line"></span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      .......</span><br><span class="line">      &#x2F;&#x2F;控制修改参数.属性对话框的显示或隐藏</span><br><span class="line">      editDialogVisible:false,</span><br><span class="line">      &#x2F;&#x2F;修改参数.属性对话框中的表单</span><br><span class="line">      editForm:&#123;</span><br><span class="line">        attr_name:&#39;&#39;</span><br><span class="line">      &#125;,</span><br><span class="line">      &#x2F;&#x2F;修改表单的验证规则</span><br><span class="line">      editFormRules:&#123;</span><br><span class="line">        attr_name:[</span><br><span class="line">          &#123; required: true, message: &#39;请输入名称&#39;, trigger: &#39;blur&#39; &#125;</span><br><span class="line">        ]</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,methods: &#123;</span><br><span class="line">    .......</span><br><span class="line">    async showEditDialog(attr_id)&#123;</span><br><span class="line">      &#x2F;&#x2F;发起请求获取需要修改的那个参数数据</span><br><span class="line">      const &#123;data:res&#125; &#x3D; await this.$http.get(&#96;categories&#x2F;$&#123;this.cateId&#125;&#x2F;attributes&#x2F;$&#123;attr_id&#125;&#96;,</span><br><span class="line">      &#123;params:&#123; attr_sel:this.activeName &#125;&#125;)</span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">        return this.$message.error(&#39;获取参数数据失败&#39;)</span><br><span class="line">      &#125;</span><br><span class="line">      this.editForm &#x3D; res.data;</span><br><span class="line">      &#x2F;&#x2F;显示修改参数.属性对话框</span><br><span class="line">      this.editDialogVisible &#x3D; true;</span><br><span class="line">    &#125;,</span><br><span class="line">    editDialogClosed()&#123;</span><br><span class="line">      &#x2F;&#x2F;当关闭修改参数.属性对话框时</span><br><span class="line">      this.$refs.editFormRef.resetFields()</span><br><span class="line">    &#125;,</span><br><span class="line">    editParams()&#123;</span><br><span class="line">      &#x2F;&#x2F;验证表单</span><br><span class="line">      this.$refs.editFormRef.validate(async valid &#x3D;&gt; &#123;</span><br><span class="line">        if(!valid) return;</span><br><span class="line"></span><br><span class="line">        &#x2F;&#x2F;发送请求完成修改</span><br><span class="line">        const &#123;data:res&#125; &#x3D; await this.$http.put(&#96;categories&#x2F;$&#123;this.cateId&#125;&#x2F;attributes&#x2F;$&#123;this.editForm.attr_id&#125;&#96;,</span><br><span class="line">        &#123;attr_name:this.editForm.attr_name,attr_sel:this.activeName&#125;)</span><br><span class="line"></span><br><span class="line">        if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">          return this.$message.error(&#39;获取参数数据失败&#39;)</span><br><span class="line">        &#125;</span><br><span class="line">        this.$message.success(&#39;修改&#39; + this.titleText + &#39;数据成功&#39;)</span><br><span class="line">        this.editDialogVisible &#x3D; false</span><br><span class="line">        this.handleChange();</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<h2 id="11-8-删除参数"><a href="#11-8-删除参数" class="headerlink" title="11.8 删除参数"></a>11.8 删除参数</h2><p>删除参数或属性</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">给两个删除按钮添加事件</span><br><span class="line">&lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;danger&quot; icon&#x3D;&quot;el-icon-delete&quot; @click&#x3D;&quot;removeParams(scope.row.attr_id)&quot;&gt;删除&lt;&#x2F;el-button&gt;</span><br><span class="line">&lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;danger&quot; icon&#x3D;&quot;el-icon-delete&quot; @click&#x3D;&quot;removeParams(scope.row.attr_id)&quot;&gt;删除&lt;&#x2F;el-button&gt;</span><br><span class="line"></span><br><span class="line">添加对应的事件处理函数</span><br><span class="line">async removeParams(attr_id)&#123;</span><br><span class="line">  &#x2F;&#x2F;根据id删除对应的参数或属性</span><br><span class="line">  &#x2F;&#x2F;弹窗提示用户是否要删除</span><br><span class="line">  const confirmResult &#x3D; await this.$confirm(</span><br><span class="line">    &#39;请问是否要删除该&#39;+this.titleText,</span><br><span class="line">    &#39;删除提示&#39;,</span><br><span class="line">    &#123;</span><br><span class="line">      confirmButtonText: &#39;确认删除&#39;,</span><br><span class="line">      cancelButtonText: &#39;取消&#39;,</span><br><span class="line">      type: &#39;warning&#39;</span><br><span class="line">    &#125;</span><br><span class="line">  ).catch(err &#x3D;&gt; err)</span><br><span class="line">  &#x2F;&#x2F;如果用户点击确认，则confirmResult 为&#39;confirm&#39;</span><br><span class="line">  &#x2F;&#x2F;如果用户点击取消, 则confirmResult获取的就是catch的错误消息&#39;cancel&#39;</span><br><span class="line">  if (confirmResult !&#x3D; &#39;confirm&#39;) &#123;</span><br><span class="line">    return this.$message.info(&#39;已经取消删除&#39;)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  &#x2F;&#x2F;没有取消就是要删除，发送请求完成删除</span><br><span class="line">      const &#123;data:res&#125; &#x3D; await  this.$http.delete(&#96;categories&#x2F;$&#123;this.cateId&#125;&#x2F;attributes&#x2F;$&#123;attr_id&#125;&#96;)</span><br><span class="line"></span><br><span class="line">    if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">    return this.$message.error(&#39;删除参数数据失败&#39;)</span><br><span class="line">    &#125;</span><br><span class="line">    this.$message.success(&#39;删除&#39; + this.titleText + &#39;数据成功&#39;)</span><br><span class="line">    this.handleChange()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="11-9-展示动态参数可选项"><a href="#11-9-展示动态参数可选项" class="headerlink" title="11.9 展示动态参数可选项"></a>11.9 展示动态参数可选项</h2><p>动态参数可选项展示及操作<br>在获取动态参数的方法中进行处理。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;将获取到的数据中的attr_vals字符串转换为数组</span><br><span class="line">res.data.forEach(item &#x3D;&gt; &#123;</span><br><span class="line">  item.attr_vals &#x3D; item.attr_vals ? item.attr_vals.split(&#39; &#39;) : []</span><br><span class="line">  &#x2F;&#x2F;添加一个bool值控制文本框的显示或者隐藏</span><br><span class="line">  item.inputVisible &#x3D; false</span><br><span class="line">  &#x2F;&#x2F;添加一个inputValue保存文本框值</span><br><span class="line">  item.inputValue &#x3D; &#39;&#39;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;然后再修改展开行中的代码，生成el-tag和文本框以及添加按钮</span><br><span class="line">&lt;!-- 展开行 --&gt;</span><br><span class="line">&lt;el-table-column type&#x3D;&quot;expand&quot;&gt;</span><br><span class="line">  &lt;template slot-scope&#x3D;&quot;scope&quot;&gt;</span><br><span class="line">    &lt;!-- 循环生成的el-tag --&gt;</span><br><span class="line">    &lt;el-tag v-for&#x3D;&quot;(item,i) in scope.row.attr_vals&quot; :key&#x3D;&quot;i&quot; closable&gt;&#123;&#123;item&#125;&#125;&lt;&#x2F;el-tag&gt;</span><br><span class="line">    &lt;!-- 输入框 --&gt;</span><br><span class="line">    &lt;el-input class&#x3D;&quot;input-new-tag&quot; v-if&#x3D;&quot;scope.row.inputVisible&quot; v-model&#x3D;&quot;scope.row.inputValue&quot; ref&#x3D;&quot;saveTagInput&quot; size&#x3D;&quot;small&quot; @keyup.enter.native&#x3D;&quot;handleInputConfirm(scope.row)&quot; @blur&#x3D;&quot;handleInputConfirm(scope.row)&quot;&gt;</span><br><span class="line">    &lt;&#x2F;el-input&gt;</span><br><span class="line">    &lt;!-- 添加按钮 --&gt;</span><br><span class="line">    &lt;el-button v-else class&#x3D;&quot;button-new-tag&quot; size&#x3D;&quot;small&quot; @click&#x3D;&quot;showInput(scope.row)&quot;&gt;+ New Tag&lt;&#x2F;el-button&gt;</span><br><span class="line">  &lt;&#x2F;template&gt;</span><br><span class="line">&lt;&#x2F;el-table-column&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;最后对应文本框的事件和按钮的事件添加处理函数</span><br><span class="line">handleInputConfirm(row)&#123;</span><br><span class="line">  &#x2F;&#x2F;当用户在文本框中按下enter键或者焦点离开时都会触发执行</span><br><span class="line">  &#x2F;&#x2F;判断用户在文本框中输入的内容是否合法</span><br><span class="line">  if(row.inputValue.trim().length&#x3D;&#x3D;&#x3D;0)&#123;</span><br><span class="line">    row.inputValue &#x3D; &#39;&#39;</span><br><span class="line">    row.inputVisible &#x3D; false</span><br><span class="line">    return</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  &#x2F;&#x2F; row.inputVisible &#x3D; false</span><br><span class="line">  &#x2F;&#x2F;如果用户输入了真实合法的数据，需要保存起来</span><br><span class="line">&#125;,</span><br><span class="line">showInput(row)&#123;</span><br><span class="line">  &#x2F;&#x2F;用户点击添加按钮时触发</span><br><span class="line">  row.inputVisible &#x3D; true</span><br><span class="line">  &#x2F;&#x2F;$nextTick:在页面上元素被重新渲染之后，调用回调函数的代码</span><br><span class="line">  this.$nextTick(_&#x3D;&gt;&#123;</span><br><span class="line">    &#x2F;&#x2F;让文本框自动获得焦点</span><br><span class="line">    this.$refs.saveTagInput.$refs.input.focus()</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="11-10-添加-删除可选项"><a href="#11-10-添加-删除可选项" class="headerlink" title="11.10 添加/删除可选项"></a>11.10 添加/删除可选项</h2><p>添加/删除动态参数可选项</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">给el-tag添加删除事件</span><br><span class="line">&lt;el-tag v-for&#x3D;&quot;(item,i) in scope.row.attr_vals&quot; :key&#x3D;&quot;i&quot; closable @close&#x3D;&quot;handleClose(i,scope.row)&quot;&gt;&#123;&#123;item&#125;&#125;&lt;&#x2F;el-tag&gt;</span><br><span class="line"></span><br><span class="line">在methods中添加新增，删除事件处理函数</span><br><span class="line">handleInputConfirm(row)&#123;</span><br><span class="line">  &#x2F;&#x2F;当用户在文本框中按下enter键或者焦点离开时都会触发执行</span><br><span class="line">  &#x2F;&#x2F;判断用户在文本框中输入的内容是否合法</span><br><span class="line">  if(row.inputValue.trim().length&#x3D;&#x3D;&#x3D;0)&#123;</span><br><span class="line">    row.inputValue &#x3D; &#39;&#39;</span><br><span class="line">    row.inputVisible &#x3D; false</span><br><span class="line">    return</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  &#x2F;&#x2F; row.inputVisible &#x3D; false</span><br><span class="line">  &#x2F;&#x2F;如果用户输入了真实合法的数据，需要保存起来</span><br><span class="line">  row.attr_vals.push(row.inputValue.trim())</span><br><span class="line">  row.inputValue &#x3D; &#39;&#39;</span><br><span class="line">  row.inputVisible &#x3D; false</span><br><span class="line"></span><br><span class="line">  this.saveAttrVals(row)</span><br><span class="line">&#125;,</span><br><span class="line">handleClose(index,row)&#123;</span><br><span class="line">  &#x2F;&#x2F;删除对应索引的参数可选项</span><br><span class="line">  row.attr_vals.splice(index,1)</span><br><span class="line">  &#x2F;&#x2F;调用函数，完成保存可选项的操作</span><br><span class="line">  this.saveAttrVals(row)</span><br><span class="line">&#125;,</span><br><span class="line">async saveAttrVals(row)&#123;</span><br><span class="line">  &#x2F;&#x2F;封装函数，完成保存可选项的操作</span><br><span class="line">  &#x2F;&#x2F;发起请求，保存参数细项</span><br><span class="line">  const &#123;data:res&#125; &#x3D; await this.$http.put(&#96;categories&#x2F;$&#123;this.cateId&#125;&#x2F;attributes&#x2F;$&#123;row.attr_id&#125;&#96;,</span><br><span class="line">  &#123;attr_name:row.attr_name,attr_sel:row.attr_sel,attr_vals:row.attr_vals.join(&#39; &#39;)&#125;)</span><br><span class="line"></span><br><span class="line">  if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">    return this.$message.error(&#39;修改参数项失败&#39;)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  this.$message.success(&#39;修改参数项成功&#39;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>补充：当用户在级联选择框中选中了非三级分类时，需要清空表格中数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">async handleChange() &#123;</span><br><span class="line">      &#x2F;&#x2F;如果用户选择的不是三级分类</span><br><span class="line">      if(this.selectedCateKeys.length !&#x3D;&#x3D; 3)&#123;</span><br><span class="line">        this.selectedCateKeys &#x3D; []</span><br><span class="line">        this.manyTableData &#x3D; []</span><br><span class="line">        this.onlyTableData &#x3D; []</span><br><span class="line">        return</span><br><span class="line">      &#125;</span><br><span class="line">      ......</span><br></pre></td></tr></table></figure>

<p>补充2：当完成了动态参数可选项的功能之后，我们也需要一样的方式完成静态属性可选项的功能。</p>
<p>此时我们只需要将动态参数可选项中的展开行复制到静态属性的表格中即可</p>
<h2 id="11-11-推送代码到码云"><a href="#11-11-推送代码到码云" class="headerlink" title="11.11 推送代码到码云"></a>11.11 推送代码到码云</h2><p>添加到暂存求： git add .<br>提交到本地仓库：  git commit -m ‘完成了分类参数开发’<br>推送到码云：  git push<br>切换到master ： git checkout master<br>合并到master ： git merge goods_params</p>
<p>推送到码云：  git push</p>
<h1 id="12-商品列表"><a href="#12-商品列表" class="headerlink" title="12.商品列表"></a>12.商品列表</h1><h2 id="12-1-创建子分支goods-list"><a href="#12-1-创建子分支goods-list" class="headerlink" title="12.1 创建子分支goods_list"></a>12.1 创建子分支goods_list</h2><p>git checkout -b goods_list<br>推送至码云 git push -u origin goods_list</p>
<h2 id="12-2-制作商品列表基本结构"><a href="#12-2-制作商品列表基本结构" class="headerlink" title="12.2 制作商品列表基本结构"></a>12.2 制作商品列表基本结构</h2><p>添加子级路由组件以及对应的规则,并设置组件的基本机构<br>打开router.js,添加下面的代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">import GoodList from &#39;.&#x2F;components&#x2F;goods&#x2F;List.vue&#39;</span><br><span class="line"></span><br><span class="line">path: &#39;&#x2F;home&#39;, component: Home, redirect: &#39;&#x2F;welcome&#39;, children: [</span><br><span class="line">  &#123; path: &quot;&#x2F;welcome&quot;, component: Welcome &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;users&quot;, component: Users &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;rights&quot;, component: Rights &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;roles&quot;, component: Roles  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;categories&quot;, component: Cate  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;params&quot;, component: Params  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;goods&quot;, component: GoodList  &#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<p>打开List.vue组件，添加下列代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;h3&gt;商品列表&lt;&#x2F;h3&gt;</span><br><span class="line">        &lt;!-- 面包屑导航 --&gt;</span><br><span class="line">        &lt;el-breadcrumb separator&#x3D;&quot;&#x2F;&quot;&gt;</span><br><span class="line">            &lt;el-breadcrumb-item :to&#x3D;&quot;&#123; path: &#39;&#x2F;home&#39; &#125;&quot;&gt;首页&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">            &lt;el-breadcrumb-item&gt;商品管理&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">            &lt;el-breadcrumb-item&gt;商品列表&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">        &lt;&#x2F;el-breadcrumb&gt;</span><br><span class="line">        &lt;!-- 卡片视图区域 --&gt;</span><br><span class="line">        &lt;el-card&gt;</span><br><span class="line">            &lt;el-row :gutter&#x3D;&quot;20&quot;&gt;</span><br><span class="line">                &lt;el-col :span&#x3D;&quot;8&quot;&gt;</span><br><span class="line">                    &lt;el-input placeholder&#x3D;&quot;请输入内容&quot;&gt;</span><br><span class="line">                        &lt;el-button slot&#x3D;&quot;append&quot; icon&#x3D;&quot;el-icon-search&quot;&gt;&lt;&#x2F;el-button&gt;</span><br><span class="line">                    &lt;&#x2F;el-input&gt;</span><br><span class="line">                &lt;&#x2F;el-col&gt;</span><br><span class="line">                &lt;el-col :span&#x3D;&quot;4&quot;&gt;</span><br><span class="line">                    &lt;el-button type&#x3D;&quot;primary&quot;&gt;添加商品&lt;&#x2F;el-button&gt;</span><br><span class="line">                &lt;&#x2F;el-col&gt;</span><br><span class="line">            &lt;&#x2F;el-row&gt;</span><br><span class="line">        &lt;&#x2F;el-card&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;&#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  created() &#123;&#125;,</span><br><span class="line">  methods: &#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style lang&#x3D;&quot;less&quot; scoped&gt;</span><br><span class="line">&lt;&#x2F;style&gt;</span><br></pre></td></tr></table></figure>

<h2 id="12-3-数据展示"><a href="#12-3-数据展示" class="headerlink" title="12.3 数据展示"></a>12.3 数据展示</h2><p>添加数据表格展示数据以及分页功能的实现,搜索功能的实现<br>在main.js中添加过滤器：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 创建过滤器将秒数过滤为年月日，时分秒</span><br><span class="line">Vue.filter(&#39;dateFormat&#39;,function(originVal)&#123;</span><br><span class="line">  const dt &#x3D; new Date(originVal)</span><br><span class="line">  const y &#x3D; dt.getFullYear()</span><br><span class="line">  const m &#x3D; (dt.getMonth()+1+&#39;&#39;).padStart(2,&#39;0&#39;)</span><br><span class="line">  const d &#x3D; (dt.getDate()+&#39;&#39;).padStart(2,&#39;0&#39;)</span><br><span class="line"></span><br><span class="line">  const hh &#x3D; (dt.getHours()+&#39;&#39;).padStart(2,&#39;0&#39;)</span><br><span class="line">  const mm &#x3D; (dt.getMinutes()+&#39;&#39;).padStart(2,&#39;0&#39;)</span><br><span class="line">  const ss &#x3D; (dt.getSeconds()+&#39;&#39;).padStart(2,&#39;0&#39;)</span><br><span class="line"></span><br><span class="line">  return &#96;$&#123;y&#125;-$&#123;m&#125;-$&#123;d&#125; $&#123;hh&#125;:$&#123;mm&#125;:$&#123;ss&#125;&#96;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 卡片视图区域 --&gt;</span><br><span class="line">&lt;el-card&gt;</span><br><span class="line">    &lt;!-- 搜索栏 --&gt;</span><br><span class="line">    &lt;el-row :gutter&#x3D;&quot;20&quot;&gt;</span><br><span class="line">        &lt;el-col :span&#x3D;&quot;8&quot;&gt;</span><br><span class="line">            &lt;el-input placeholder&#x3D;&quot;请输入内容&quot; v-model&#x3D;&quot;queryInfo.query&quot; clearable @clear&#x3D;&quot;getGoodsList&quot;&gt;</span><br><span class="line">                &lt;el-button slot&#x3D;&quot;append&quot; icon&#x3D;&quot;el-icon-search&quot; @click&#x3D;&quot;getGoodsList&quot;&gt;&lt;&#x2F;el-button&gt;</span><br><span class="line">            &lt;&#x2F;el-input&gt;</span><br><span class="line">        &lt;&#x2F;el-col&gt;</span><br><span class="line">        &lt;el-col :span&#x3D;&quot;4&quot;&gt;</span><br><span class="line">            &lt;el-button type&#x3D;&quot;primary&quot;&gt;添加商品&lt;&#x2F;el-button&gt;</span><br><span class="line">        &lt;&#x2F;el-col&gt;</span><br><span class="line">    &lt;&#x2F;el-row&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!-- 表格区域 --&gt;</span><br><span class="line">    &lt;el-table :data&#x3D;&quot;goodsList&quot; border stripe&gt;</span><br><span class="line">        &lt;el-table-column type&#x3D;&quot;index&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">        &lt;el-table-column label&#x3D;&quot;商品名称&quot; prop&#x3D;&quot;goods_name&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">        &lt;el-table-column label&#x3D;&quot;商品价格(元)&quot; prop&#x3D;&quot;goods_price&quot; width&#x3D;&quot;95px&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">        &lt;el-table-column label&#x3D;&quot;商品重量&quot; prop&#x3D;&quot;goods_weight&quot; width&#x3D;&quot;95px&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">        &lt;el-table-column label&#x3D;&quot;创建时间&quot; prop&#x3D;&quot;add_time&quot; width&#x3D;&quot;140px&quot;&gt;</span><br><span class="line">            &lt;template slot-scope&#x3D;&quot;scope&quot;&gt;</span><br><span class="line">                &#123;&#123;scope.row.add_time | dateFormat&#125;&#125;</span><br><span class="line">            &lt;&#x2F;template&gt;</span><br><span class="line">        &lt;&#x2F;el-table-column&gt;</span><br><span class="line">        &lt;el-table-column label&#x3D;&quot;操作&quot; width&#x3D;&quot;125px&quot;&gt;</span><br><span class="line">            &lt;template slot-scope&#x3D;&quot;scope&quot;&gt;</span><br><span class="line">                &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;primary&quot; icon&#x3D;&quot;el-icon-edit&quot;&gt;&lt;&#x2F;el-button&gt;</span><br><span class="line">                &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;danger&quot; icon&#x3D;&quot;el-icon-delete&quot;&gt;&lt;&#x2F;el-button&gt;</span><br><span class="line">            &lt;&#x2F;template&gt;</span><br><span class="line">        &lt;&#x2F;el-table-column&gt;</span><br><span class="line">    &lt;&#x2F;el-table&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!-- 分页 --&gt;</span><br><span class="line">    &lt;el-pagination @size-change&#x3D;&quot;handleSizeChange&quot; @current-change&#x3D;&quot;handleCurrentChange&quot; :current-page&#x3D;&quot;queryInfo.pagenum&quot; :page-sizes&#x3D;&quot;[3, 5, 10, 15]&quot; :page-size&#x3D;&quot;queryInfo.pagesize&quot; layout&#x3D;&quot;total, sizes, prev, pager, next, jumper&quot; :total&#x3D;&quot;total&quot;&gt;</span><br><span class="line">    &lt;&#x2F;el-pagination&gt;</span><br><span class="line">&lt;&#x2F;el-card&gt;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;绑定数据以及添加方法</span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      &#x2F;&#x2F; 查询参数</span><br><span class="line">      queryInfo: &#123;</span><br><span class="line">        query: &#39;&#39;,</span><br><span class="line">        pagenum: 1,</span><br><span class="line">        pagesize: 10</span><br><span class="line">      &#125;,</span><br><span class="line">      &#x2F;&#x2F; 保存商品列表信息</span><br><span class="line">      goodsList: [],</span><br><span class="line">      &#x2F;&#x2F; 总数据条数</span><br><span class="line">      total: 0</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  created() &#123;</span><br><span class="line">    this.getGoodsList()</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    async getGoodsList() &#123;</span><br><span class="line">      &#x2F;&#x2F; 根据分页获取对应的商品列表</span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.get(&#39;goods&#39;, &#123;</span><br><span class="line">        params: this.queryInfo</span><br><span class="line">      &#125;)</span><br><span class="line"></span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">        return this.$message.error(&#39;获取商品列表失败&#39;)</span><br><span class="line">      &#125;</span><br><span class="line">      console.log(res.data)</span><br><span class="line">      this.$message.success(&#39;获取商品列表成功&#39;)</span><br><span class="line">      this.goodsList &#x3D; res.data.goods</span><br><span class="line">      this.total &#x3D; res.data.total</span><br><span class="line">    &#125;,</span><br><span class="line">    handleSizeChange(newSize)&#123;</span><br><span class="line">        &#x2F;&#x2F; 当页号发生改变时，更改pagesize，重新请求</span><br><span class="line">        this.queryInfo.pagesize &#x3D; newSize</span><br><span class="line">        this.getGoodsList();</span><br><span class="line">    &#125;,</span><br><span class="line">    handleCurrentChange(newPage)&#123;</span><br><span class="line">        &#x2F;&#x2F; 当页码发生改变时，更改pagesize，重新请求</span><br><span class="line">        this.queryInfo.pagenum &#x3D; newPage</span><br><span class="line">        this.getGoodsList();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<h2 id="12-4-实现删除商品"><a href="#12-4-实现删除商品" class="headerlink" title="12.4 实现删除商品"></a>12.4 实现删除商品</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 绑定按钮点击事件</span><br><span class="line">&lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;danger&quot; icon&#x3D;&quot;el-icon-delete&quot; @click&#x3D;&quot;removeGoods(scope.row.goods_id)&quot;&gt;&lt;&#x2F;el-button&gt;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 事件函数代码编写</span><br><span class="line">async removeGoods(goods_id) &#123;</span><br><span class="line">  &#x2F;&#x2F; 根据id删除对应的参数或属性</span><br><span class="line">  &#x2F;&#x2F; 弹窗提示用户是否要删除</span><br><span class="line">  const confirmResult &#x3D; await this.$confirm(</span><br><span class="line">    &#39;请问是否要删除该商品&#39;,</span><br><span class="line">    &#39;删除提示&#39;,</span><br><span class="line">    &#123;</span><br><span class="line">      confirmButtonText: &#39;确认删除&#39;,</span><br><span class="line">      cancelButtonText: &#39;取消&#39;,</span><br><span class="line">      type: &#39;warning&#39;</span><br><span class="line">    &#125;</span><br><span class="line">  ).catch(err &#x3D;&gt; err)</span><br><span class="line">  &#x2F;&#x2F; 如果用户点击确认，则confirmResult 为&#39;confirm&#39;</span><br><span class="line">  &#x2F;&#x2F; 如果用户点击取消, 则confirmResult获取的就是catch的错误消息&#39;cancel&#39;</span><br><span class="line">  if (confirmResult !&#x3D; &#39;confirm&#39;) &#123;</span><br><span class="line">    return this.$message.info(&#39;已经取消删除&#39;)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  &#x2F;&#x2F; 没有取消就是要删除，发送请求完成删除</span><br><span class="line">  const &#123;data:res&#125; &#x3D; await this.$http.delete(&#96;goods&#x2F;$&#123;goods_id&#125;&#96;)</span><br><span class="line"></span><br><span class="line">  if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">    return this.$message.error(&#39;删除商品失败&#39;)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  this.$message.success(&#39;删除商品成功&#39;)</span><br><span class="line">  this.getGoodsList()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="12-5-实现添加商品"><a href="#12-5-实现添加商品" class="headerlink" title="12.5 实现添加商品"></a>12.5 实现添加商品</h2><h3 id="12-5-1-添加编程式导航"><a href="#12-5-1-添加编程式导航" class="headerlink" title="12.5.1 添加编程式导航"></a>12.5.1 添加编程式导航</h3><p>在List.vue中添加编程式导航，并创建添加商品路由组件及规则</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;在List.vue中添加编程式导航</span><br><span class="line">&lt;el-col :span&#x3D;&quot;4&quot;&gt;</span><br><span class="line">    &lt;el-button type&#x3D;&quot;primary&quot; @click&#x3D;&quot;goAddPage&quot;&gt;添加商品&lt;&#x2F;el-button&gt;</span><br><span class="line">&lt;&#x2F;el-col&gt;</span><br><span class="line"></span><br><span class="line">goAddPage()&#123;</span><br><span class="line">    this.$router.push(&#39;&#x2F;goods&#x2F;add&#39;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在router.js中引入goods/Add.vue,并添加路由规则</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">import GoodAdd from &#39;.&#x2F;components&#x2F;goods&#x2F;Add.vue&#39;</span><br><span class="line">path: &#39;&#x2F;home&#39;, component: Home, redirect: &#39;&#x2F;welcome&#39;, children: [</span><br><span class="line">  &#123; path: &quot;&#x2F;welcome&quot;, component: Welcome &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;users&quot;, component: Users &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;rights&quot;, component: Rights &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;roles&quot;, component: Roles  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;categories&quot;, component: Cate  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;params&quot;, component: Params  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;goods&quot;, component: GoodList  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;goods&#x2F;add&quot;, component: GoodAdd  &#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<h3 id="12-5-2-布局Add-vue组件"><a href="#12-5-2-布局Add-vue组件" class="headerlink" title="12.5.2 布局Add.vue组件"></a>12.5.2 布局Add.vue组件</h3><p>布局过程中需要使用Steps组件，在element.js中引入并注册该组件，并在global.css中给组件设置全局样式</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">import &#123;Steps,Step&#125; from &#39;element-ui&#39;</span><br><span class="line">Vue.use(Step)</span><br><span class="line">Vue.use(Steps)</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;global.css</span><br><span class="line">.el-steps&#123;</span><br><span class="line">    margin:15px 0;</span><br><span class="line">&#125;</span><br><span class="line">.el-step__title&#123;</span><br><span class="line">    font-size: 13px;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后再在Add.vue中进行页面布局</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;h3&gt;添加商品&lt;&#x2F;h3&gt;</span><br><span class="line">        &lt;!-- 面包屑导航 --&gt;</span><br><span class="line">        &lt;el-breadcrumb separator&#x3D;&quot;&#x2F;&quot;&gt;</span><br><span class="line">            &lt;el-breadcrumb-item :to&#x3D;&quot;&#123; path: &#39;&#x2F;home&#39; &#125;&quot;&gt;首页&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">            &lt;el-breadcrumb-item&gt;商品管理&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">            &lt;el-breadcrumb-item&gt;添加商品&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">        &lt;&#x2F;el-breadcrumb&gt;</span><br><span class="line">        &lt;!-- 卡片视图区域 --&gt;</span><br><span class="line">        &lt;el-card&gt;</span><br><span class="line">            &lt;!-- 消息提示 --&gt;</span><br><span class="line">            &lt;el-alert title&#x3D;&quot;添加商品信息&quot; type&#x3D;&quot;info&quot; center show-icon :closable&#x3D;&quot;false&quot;&gt;</span><br><span class="line">            &lt;&#x2F;el-alert&gt;</span><br><span class="line"></span><br><span class="line">            &lt;!-- 步骤条组件 --&gt;</span><br><span class="line">            &lt;!-- align-center(居中效果) --&gt;</span><br><span class="line">            &lt;el-steps :space&#x3D;&quot;200&quot; :active&#x3D;&quot;activeIndex - 0&quot; finish-status&#x3D;&quot;success&quot; align-center&gt;</span><br><span class="line">                &lt;el-step title&#x3D;&quot;基本信息&quot;&gt;&lt;&#x2F;el-step&gt;</span><br><span class="line">                &lt;el-step title&#x3D;&quot;商品参数&quot;&gt;&lt;&#x2F;el-step&gt;</span><br><span class="line">                &lt;el-step title&#x3D;&quot;商品属性&quot;&gt;&lt;&#x2F;el-step&gt;</span><br><span class="line">                &lt;el-step title&#x3D;&quot;商品图片&quot;&gt;&lt;&#x2F;el-step&gt;</span><br><span class="line">                &lt;el-step title&#x3D;&quot;商品内容&quot;&gt;&lt;&#x2F;el-step&gt;</span><br><span class="line">                &lt;el-step title&#x3D;&quot;完成&quot;&gt;&lt;&#x2F;el-step&gt;</span><br><span class="line">            &lt;&#x2F;el-steps&gt;</span><br><span class="line"></span><br><span class="line">            &lt;!-- tab栏区域:el-tab-pane必须是el-tabs的子节点</span><br><span class="line">            :tab-position&#x3D;&quot;&#39;left&#39;&quot;(设置tab栏为左右结构tab栏) --&gt;</span><br><span class="line">            &lt;!-- 表单：label-position&#x3D;&quot;top&quot;(设置label在文本框上方) --&gt;</span><br><span class="line">            &lt;el-form :model&#x3D;&quot;addForm&quot; :rules&#x3D;&quot;addFormRules&quot; ref&#x3D;&quot;addFormRef&quot; label-width&#x3D;&quot;100px&quot; label-position&#x3D;&quot;top&quot;&gt;</span><br><span class="line">                &lt;el-tabs v-model&#x3D;&quot;activeIndex&quot; :tab-position&#x3D;&quot;&#39;left&#39;&quot;&gt;</span><br><span class="line">                    &lt;el-tab-pane label&#x3D;&quot;基本信息&quot; name&#x3D;&quot;0&quot;&gt;</span><br><span class="line">                        &lt;el-form-item label&#x3D;&quot;商品名称&quot; prop&#x3D;&quot;goods_name&quot;&gt;</span><br><span class="line">                            &lt;el-input v-model&#x3D;&quot;addForm.goods_name&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">                        &lt;&#x2F;el-form-item&gt;</span><br><span class="line">                        &lt;el-form-item label&#x3D;&quot;商品价格&quot; prop&#x3D;&quot;goods_price&quot;&gt;</span><br><span class="line">                            &lt;el-input v-model&#x3D;&quot;addForm.goods_price&quot; type&#x3D;&quot;number&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">                        &lt;&#x2F;el-form-item&gt;</span><br><span class="line">                        &lt;el-form-item label&#x3D;&quot;商品重量&quot; prop&#x3D;&quot;goods_weight&quot;&gt;</span><br><span class="line">                            &lt;el-input v-model&#x3D;&quot;addForm.goods_weight&quot; type&#x3D;&quot;number&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">                        &lt;&#x2F;el-form-item&gt;</span><br><span class="line">                        &lt;el-form-item label&#x3D;&quot;商品数量&quot; prop&#x3D;&quot;goods_number&quot;&gt;</span><br><span class="line">                            &lt;el-input v-model&#x3D;&quot;addForm.goods_number&quot; type&#x3D;&quot;number&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">                        &lt;&#x2F;el-form-item&gt;</span><br><span class="line">                        &lt;el-form-item label&#x3D;&quot;商品分类&quot; prop&#x3D;&quot;goods_cat&quot;&gt;</span><br><span class="line">                            &lt;!-- 选择商品分类的级联选择框 --&gt;</span><br><span class="line">                            &lt;el-cascader expandTrigger&#x3D;&#39;hover&#39; v-model&#x3D;&quot;addForm.goods_cat&quot; :options&#x3D;&quot;cateList&quot; :props&#x3D;&quot;cateProps&quot; @change&#x3D;&quot;handleChange&quot; clearable&gt;&lt;&#x2F;el-cascader&gt;</span><br><span class="line">                        &lt;&#x2F;el-form-item&gt;</span><br><span class="line">                    &lt;&#x2F;el-tab-pane&gt;</span><br><span class="line">                    &lt;el-tab-pane label&#x3D;&quot;商品参数&quot; name&#x3D;&quot;1&quot;&gt;</span><br><span class="line"></span><br><span class="line">                    &lt;&#x2F;el-tab-pane&gt;</span><br><span class="line">                    &lt;el-tab-pane label&#x3D;&quot;商品属性&quot; name&#x3D;&quot;2&quot;&gt;</span><br><span class="line"></span><br><span class="line">                    &lt;&#x2F;el-tab-pane&gt;</span><br><span class="line">                    &lt;el-tab-pane label&#x3D;&quot;商品图片&quot; name&#x3D;&quot;3&quot;&gt;</span><br><span class="line"></span><br><span class="line">                    &lt;&#x2F;el-tab-pane&gt;</span><br><span class="line">                    &lt;el-tab-pane label&#x3D;&quot;商品内容&quot; name&#x3D;&quot;4&quot;&gt;</span><br><span class="line"></span><br><span class="line">                    &lt;&#x2F;el-tab-pane&gt;</span><br><span class="line">                &lt;&#x2F;el-tabs&gt;</span><br><span class="line">            &lt;&#x2F;el-form&gt;</span><br><span class="line">        &lt;&#x2F;el-card&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      &#x2F;&#x2F;保存步骤条激活项索引</span><br><span class="line">      activeIndex: &#39;0&#39;,</span><br><span class="line">      &#x2F;&#x2F;添加商品的表单数据对象</span><br><span class="line">      addForm: &#123;</span><br><span class="line">        goods_name: &#39;&#39;,</span><br><span class="line">        goods_price: 0,</span><br><span class="line">        goods_weight: 0,</span><br><span class="line">        goods_number: 0,</span><br><span class="line">        goods_cat:[]</span><br><span class="line">      &#125;,</span><br><span class="line">      &#x2F;&#x2F;验证规则</span><br><span class="line">      addFormRules: &#123;</span><br><span class="line">        goods_name: [</span><br><span class="line">          &#123; required: true, message: &#39;请输入商品名称&#39;, trigger: &#39;blur&#39; &#125;</span><br><span class="line">        ],</span><br><span class="line">        goods_price: [</span><br><span class="line">          &#123; required: true, message: &#39;请输入商品价格&#39;, trigger: &#39;blur&#39; &#125;</span><br><span class="line">        ],</span><br><span class="line">        goods_weight: [</span><br><span class="line">          &#123; required: true, message: &#39;请输入商品重量&#39;, trigger: &#39;blur&#39; &#125;</span><br><span class="line">        ],</span><br><span class="line">        goods_number: [</span><br><span class="line">          &#123; required: true, message: &#39;请输入商品数量&#39;, trigger: &#39;blur&#39; &#125;</span><br><span class="line">        ],</span><br><span class="line">        goods_cat: [</span><br><span class="line">          &#123; required: true, message: &#39;请选择商品分类&#39;, trigger: &#39;blur&#39; &#125;</span><br><span class="line">        ]</span><br><span class="line">      &#125;,</span><br><span class="line">      &#x2F;&#x2F;用来保存分类数据</span><br><span class="line">      cateList: [],</span><br><span class="line">      &#x2F;&#x2F;配置级联菜单中数据如何展示</span><br><span class="line">      cateProps: &#123;</span><br><span class="line">        value: &#39;cat_id&#39;,</span><br><span class="line">        label: &#39;cat_name&#39;,</span><br><span class="line">        children: &#39;children&#39;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  created() &#123;</span><br><span class="line">    this.getCateList()</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    async getCateList() &#123;</span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.get(&#39;categories&#39;)</span><br><span class="line"></span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">        return this.$message.error(&#39;获取商品分类数据失败&#39;)</span><br><span class="line">      &#125;</span><br><span class="line">      this.cateList &#x3D; res.data</span><br><span class="line">    &#125;,</span><br><span class="line">    handleChange()&#123;</span><br><span class="line">      &#x2F;&#x2F;如果用户选择的不是三级分类,该次选择无效，因为必须选择三级分类</span><br><span class="line">      if(this.addForm.goods_cat.length !&#x3D;&#x3D; 3)&#123;</span><br><span class="line">        this.addForm.goods_cat &#x3D; []</span><br><span class="line">        return</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style lang&#x3D;&quot;less&quot; scoped&gt;</span><br><span class="line">&lt;&#x2F;style&gt;</span><br></pre></td></tr></table></figure>

<h3 id="12-5-3-添加tab栏切换验证"><a href="#12-5-3-添加tab栏切换验证" class="headerlink" title="12.5.3 添加tab栏切换验证"></a>12.5.3 添加tab栏切换验证</h3><p>也就是说不输入某些内容，无法切换到别的tab栏</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;首先给tabs添加tab切换前事件</span><br><span class="line">&lt;el-tabs v-model&#x3D;&quot;activeIndex&quot; :tab-position&#x3D;&quot;&#39;left&#39;&quot; :before-leave&#x3D;&quot;beforeTabLeave&quot;&gt;</span><br><span class="line">......</span><br><span class="line">&lt;&#x2F;el-tabs&gt;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;再到methods编写事件函数beforeTabLeave</span><br><span class="line">beforeTabLeave(activeName,oldActiveName)&#123;</span><br><span class="line">  &#x2F;&#x2F;在tab栏切换之前触发，两个形参为切换前，后的tab栏name</span><br><span class="line">  if(oldActiveName &#x3D;&#x3D;&#x3D; &#39;0&#39;)&#123;</span><br><span class="line">      &#x2F;&#x2F;在第一个标签页的时候</span><br><span class="line">      if(this.addForm.goods_cat.length !&#x3D;&#x3D; 3)&#123;</span><br><span class="line">          this.$message.error(&#39;请选择商品的分类&#39;)</span><br><span class="line">          return false</span><br><span class="line">      &#125;else if(this.addForm.goods_name.trim() &#x3D;&#x3D;&#x3D; &#39;&#39;)&#123;</span><br><span class="line">          this.$message.error(&#39;请输入商品名称&#39;)</span><br><span class="line">          return false</span><br><span class="line">      &#125;else if(this.addForm.goods_price.trim() &#x3D;&#x3D;&#x3D; &#39;0&#39;)&#123;</span><br><span class="line">          this.$message.error(&#39;请输入商品价格&#39;)</span><br><span class="line">          return false</span><br><span class="line">      &#125;else if(this.addForm.goods_weight.trim() &#x3D;&#x3D;&#x3D; &#39;0&#39;)&#123;</span><br><span class="line">          this.$message.error(&#39;请输入商品重量&#39;)</span><br><span class="line">          return false</span><br><span class="line">      &#125;else if(this.addForm.goods_number.trim() &#x3D;&#x3D;&#x3D; &#39;0&#39;)&#123;</span><br><span class="line">          this.$message.error(&#39;请输入商品数量&#39;)</span><br><span class="line">          return false</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="12-5-4-展示信息"><a href="#12-5-4-展示信息" class="headerlink" title="12.5.4 展示信息"></a>12.5.4 展示信息</h3><p>展示商品参数信息,商品属性信息<br>在商品参数信息展示中使用的el-checkbox,el-checkbox-group组件，打开element.js引入组件并注册组件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;在用户点击tab栏时触发事件</span><br><span class="line">&lt;el-tabs v-model&#x3D;&quot;activeIndex&quot; :tab-position&#x3D;&quot;&#39;left&#39;&quot; :before-leave&#x3D;&quot;beforeTabLeave&quot; @tab-click&#x3D;&quot;tabClicked&quot;&gt;</span><br><span class="line">........</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;在参数信息，商品属性面板中添加循环生成结构的代码</span><br><span class="line">&lt;el-tab-pane label&#x3D;&quot;商品参数&quot; name&#x3D;&quot;1&quot;&gt;</span><br><span class="line">  &lt;!-- 渲染表单item项 --&gt;</span><br><span class="line">  &lt;el-form-item :label&#x3D;&quot;item.attr_name&quot; :key&#x3D;&quot;item.attr_id&quot; v-for&#x3D;&quot;item in manyTableData&quot;&gt;</span><br><span class="line">      &lt;!-- 使用数组渲染复选框组 --&gt;</span><br><span class="line">      &lt;el-checkbox-group v-model&#x3D;&quot;item.attr_vals&quot;&gt;</span><br><span class="line">          &lt;el-checkbox border :label&#x3D;&quot;val&quot; v-for&#x3D;&quot;(val,i) in item.attr_vals&quot; :key&#x3D;&quot;i&quot;&gt;&lt;&#x2F;el-checkbox&gt;</span><br><span class="line">      &lt;&#x2F;el-checkbox-group&gt;</span><br><span class="line">  &lt;&#x2F;el-form-item&gt;</span><br><span class="line">&lt;&#x2F;el-tab-pane&gt;</span><br><span class="line">&lt;el-tab-pane label&#x3D;&quot;商品属性&quot; name&#x3D;&quot;2&quot;&gt;</span><br><span class="line">  &lt;!-- 循环生成静态属性 --&gt;</span><br><span class="line">  &lt;el-form-item :label&#x3D;&quot;item.attr_name&quot; v-for&#x3D;&quot;item in onlyTableData&quot; :key&#x3D;&quot;item.attr_id&quot;&gt;</span><br><span class="line">      &lt;el-input v-model&#x3D;&quot;item.attr_vals&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">  &lt;&#x2F;el-form-item&gt;</span><br><span class="line">&lt;&#x2F;el-tab-pane&gt;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;在data数据中添加保存动态参数和静态属性的数组</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      ......</span><br><span class="line">      &#x2F;&#x2F;动态参数列表</span><br><span class="line">      manyTableData: [],</span><br><span class="line">      &#x2F;&#x2F;静态属性列表</span><br><span class="line">      onlyTableData:[]</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;,methods: &#123;</span><br><span class="line">    .......</span><br><span class="line">    async tabClicked() &#123;</span><br><span class="line">      &#x2F;&#x2F;当用户点击切换tab栏时触发</span><br><span class="line">      if (this.activeIndex &#x3D;&#x3D;&#x3D; &#39;1&#39;) &#123;</span><br><span class="line">        &#x2F;&#x2F;发送请求获取动态参数</span><br><span class="line">        const &#123; data: res &#125; &#x3D; await this.$http.get(</span><br><span class="line">          &#96;categories&#x2F;$&#123;this.cateId&#125;&#x2F;attributes&#96;,</span><br><span class="line">          &#123; params: &#123; sel: &#39;many&#39; &#125; &#125;</span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">        if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">          return this.$message.error(&#39;获取动态参数列表失败&#39;)</span><br><span class="line">        &#125;</span><br><span class="line">        &#x2F;&#x2F;将attr_vals字符串转换为数组</span><br><span class="line">        res.data.forEach(item &#x3D;&gt; &#123;</span><br><span class="line">          item.attr_vals &#x3D;</span><br><span class="line">            item.attr_vals.length &#x3D;&#x3D;&#x3D; 0 ? [] : item.attr_vals.split(&#39; &#39;)</span><br><span class="line">        &#125;)</span><br><span class="line">        this.manyTableData &#x3D; res.data</span><br><span class="line">      &#125; else if (this.activeIndex &#x3D;&#x3D;&#x3D; &#39;2&#39;) &#123;</span><br><span class="line">        &#x2F;&#x2F;发送请求获取静态属性</span><br><span class="line">        const &#123; data: res &#125; &#x3D; await this.$http.get(</span><br><span class="line">          &#96;categories&#x2F;$&#123;this.cateId&#125;&#x2F;attributes&#96;,</span><br><span class="line">          &#123; params: &#123; sel: &#39;only&#39; &#125; &#125;</span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">        if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">          return this.$message.error(&#39;获取静态属性列表失败&#39;)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        this.onlyTableData &#x3D; res.data</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  &#x2F;&#x2F;添加 计算属性获取三级分类</span><br><span class="line">  computed: &#123;</span><br><span class="line">    cateId() &#123;</span><br><span class="line">      if (this.addForm.goods_cat.length &#x3D;&#x3D;&#x3D; 3) &#123;</span><br><span class="line">        return this.addForm.goods_cat[2]</span><br><span class="line">      &#125;</span><br><span class="line">      return null</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="12-5-5-完成图片上传"><a href="#12-5-5-完成图片上传" class="headerlink" title="12.5.5 完成图片上传"></a>12.5.5 完成图片上传</h3><p>使用upload组件完成图片上传<br>在element.js中引入upload组件，并注册<br>因为upload组件进行图片上传的时候并不是使用axios发送请求<br>所以，我们需要手动为上传图片的请求添加token，即为upload组件添加headers属性</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;在页面中添加upload组件，并设置对应的事件和属性</span><br><span class="line">&lt;el-tab-pane label&#x3D;&quot;商品图片&quot; name&#x3D;&quot;3&quot;&gt;</span><br><span class="line">  &lt;!-- 商品图片上传</span><br><span class="line">  action:指定图片上传api接口</span><br><span class="line">  :on-preview ： 当点击图片时会触发该事件进行预览操作,处理图片预览</span><br><span class="line">  :on-remove : 当用户点击图片右上角的X号时触发执行</span><br><span class="line">  :on-success：当用户点击上传图片并成功上传时触发</span><br><span class="line">  list-type ：设置预览图片的方式</span><br><span class="line">  :headers ：设置上传图片的请求头 --&gt;</span><br><span class="line">  &lt;el-upload :action&#x3D;&quot;uploadURL&quot; :on-preview&#x3D;&quot;handlePreview&quot; :on-remove&#x3D;&quot;handleRemove&quot; :on-success&#x3D;&quot;handleSuccess&quot; list-type&#x3D;&quot;picture&quot; :headers&#x3D;&quot;headerObj&quot;&gt;</span><br><span class="line">    &lt;el-button size&#x3D;&quot;small&quot; type&#x3D;&quot;primary&quot;&gt;点击上传&lt;&#x2F;el-button&gt;</span><br><span class="line">  &lt;&#x2F;el-upload&gt;</span><br><span class="line">&lt;&#x2F;el-tab-pane&gt;</span><br><span class="line">&#x2F;&#x2F;在el-card卡片视图下面添加对话框用来预览图片</span><br><span class="line">&lt;!-- 预览图片对话框 --&gt;</span><br><span class="line">&lt;el-dialog title&#x3D;&quot;图片预览&quot; :visible.sync&#x3D;&quot;previewVisible&quot; width&#x3D;&quot;50%&quot;&gt;</span><br><span class="line">  &lt;img :src&#x3D;&quot;previewPath&quot; class&#x3D;&quot;previewImg&quot; &#x2F;&gt;</span><br><span class="line">&lt;&#x2F;el-dialog&gt;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;在data中添加数据</span><br><span class="line">data()&#123;</span><br><span class="line">  return &#123;</span><br><span class="line">    ......</span><br><span class="line">    &#x2F;&#x2F;添加商品的表单数据对象</span><br><span class="line">    addForm: &#123;</span><br><span class="line">      goods_name: &#39;&#39;,</span><br><span class="line">      goods_price: 0,</span><br><span class="line">      goods_weight: 0,</span><br><span class="line">      goods_number: 0,</span><br><span class="line">      goods_cat: [],</span><br><span class="line">      &#x2F;&#x2F;上传图片数组</span><br><span class="line">      pics: []</span><br><span class="line">    &#125;,</span><br><span class="line">    &#x2F;&#x2F;上传图片的url地址</span><br><span class="line">    uploadURL: &#39;http:&#x2F;&#x2F;127.0.0.1:8888&#x2F;api&#x2F;private&#x2F;v1&#x2F;upload&#39;,</span><br><span class="line">    &#x2F;&#x2F;图片上传组件的headers请求头对象</span><br><span class="line">    headerObj: &#123; Authorization: window.sessionStorage.getItem(&#39;token&#39;) &#125;,</span><br><span class="line">    &#x2F;&#x2F;保存预览图片的url地址</span><br><span class="line">    previewPath: &#39;&#39;,</span><br><span class="line">    &#x2F;&#x2F;控制预览图片对话框的显示和隐藏</span><br><span class="line">    previewVisible:false</span><br><span class="line">  &#125;</span><br><span class="line">&#125;,</span><br><span class="line">&#x2F;&#x2F;在methods中添加事件处理函数</span><br><span class="line">methods:&#123;</span><br><span class="line">  .......</span><br><span class="line">  handlePreview(file) &#123;</span><br><span class="line">    &#x2F;&#x2F;当用户点击图片进行预览时执行，处理图片预览</span><br><span class="line">    &#x2F;&#x2F;形参file就是用户预览的那个文件</span><br><span class="line">    this.previewPath &#x3D; file.response.data.url</span><br><span class="line">    &#x2F;&#x2F;显示预览图片对话框</span><br><span class="line">    this.previewVisible &#x3D; true</span><br><span class="line">  &#125;,</span><br><span class="line">  handleRemove(file) &#123;</span><br><span class="line">    &#x2F;&#x2F;当用户点击X号删除时执行</span><br><span class="line">    &#x2F;&#x2F;形参file就是用户点击删除的文件</span><br><span class="line">    &#x2F;&#x2F;获取用户点击删除的那个图片的临时路径</span><br><span class="line">    const filePath &#x3D; file.response.data.tmp_path</span><br><span class="line">    &#x2F;&#x2F;使用findIndex来查找符合条件的索引</span><br><span class="line">    const index &#x3D; this.addForm.pics.findIndex(item &#x3D;&gt; item.pic &#x3D;&#x3D;&#x3D; filePath)</span><br><span class="line">    &#x2F;&#x2F;移除索引对应的图片</span><br><span class="line">    this.addForm.pics.splice(index, 1)</span><br><span class="line">  &#125;,</span><br><span class="line">  handleSuccess(response) &#123;</span><br><span class="line">    &#x2F;&#x2F;当上传成功时触发执行</span><br><span class="line">    &#x2F;&#x2F;形参response就是上传成功之后服务器返回的结果</span><br><span class="line">    &#x2F;&#x2F;将服务器返回的临时路径保存到addForm表单的pics数组中</span><br><span class="line">    this.addForm.pics.push(&#123; pic: response.data.tmp_path &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="12-5-6-使用富文本插件"><a href="#12-5-6-使用富文本插件" class="headerlink" title="12.5.6 使用富文本插件"></a>12.5.6 使用富文本插件</h3><p>想要使用富文本插件vue-quill-editor，就必须先从依赖安装该插件<br>引入并注册vue-quill-editor，打开main.js，编写如下代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;导入vue-quill-editor（富文本编辑器）</span><br><span class="line">import VueQuillEditor from &#39;vue-quill-editor&#39;</span><br><span class="line">&#x2F;&#x2F;导入vue-quill-editor的样式</span><br><span class="line">import &#39;quill&#x2F;dist&#x2F;quill.core.css&#39;</span><br><span class="line">import &#39;quill&#x2F;dist&#x2F;quill.snow.css&#39;</span><br><span class="line">import &#39;quill&#x2F;dist&#x2F;quill.bubble.css&#39;</span><br><span class="line">......</span><br><span class="line">&#x2F;&#x2F;全局注册组件</span><br><span class="line">Vue.component(&#39;tree-table&#39;, TreeTable)</span><br><span class="line">&#x2F;&#x2F;全局注册富文本组件</span><br><span class="line">Vue.use(VueQuillEditor)</span><br></pre></td></tr></table></figure>

<p>使用富文本插件vue-quill-editor</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 富文本编辑器组件 --&gt;</span><br><span class="line">&lt;el-tab-pane label&#x3D;&quot;商品内容&quot; name&#x3D;&quot;4&quot;&gt;</span><br><span class="line">  &lt;!-- 富文本编辑器组件 --&gt;</span><br><span class="line">  &lt;quill-editor v-model&#x3D;&quot;addForm.goods_introduce&quot;&gt;&lt;&#x2F;quill-editor&gt;</span><br><span class="line">  &lt;!-- 添加商品按钮 --&gt;</span><br><span class="line">  &lt;el-button type&#x3D;&quot;primary&quot; class&#x3D;&quot;btnAdd&quot;&gt;添加商品&lt;&#x2F;el-button&gt;</span><br><span class="line">&lt;&#x2F;el-tab-pane&gt;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;在数据中添加goods_introduce</span><br><span class="line">&#x2F;&#x2F;添加商品的表单数据对象</span><br><span class="line">addForm: &#123;</span><br><span class="line">  goods_name: &#39;&#39;,</span><br><span class="line">  goods_price: 0,</span><br><span class="line">  goods_weight: 0,</span><br><span class="line">  goods_number: 0,</span><br><span class="line">  goods_cat: [],</span><br><span class="line">  &#x2F;&#x2F;上传图片数组</span><br><span class="line">  pics: [],</span><br><span class="line">  &#x2F;&#x2F;商品的详情介绍</span><br><span class="line">  goods_introduce:&#39;&#39;</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F;在global.css样式中添加富文本编辑器的最小高度</span><br><span class="line">.ql-editor&#123;</span><br><span class="line">    min-height: 300px;</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F;给添加商品按钮添加间距</span><br><span class="line">.btnAdd&#123;</span><br><span class="line">  margin-top:15px;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="12-6-添加商品"><a href="#12-6-添加商品" class="headerlink" title="12.6 添加商品"></a>12.6 添加商品</h2><p>完成添加商品的操作<br>在添加商品之前，为了避免goods_cat数组转换字符串之后导致级联选择器报错<br>我们需要打开vue控制条，点击依赖，安装lodash，把addForm进行深拷贝</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;打开Add.vue，导入lodash</span><br><span class="line">&lt;script&gt;</span><br><span class="line">&#x2F;&#x2F;官方推荐将lodash导入为_</span><br><span class="line">import _ from &#39;lodash&#39;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;给添加商品按钮绑定点击事件</span><br><span class="line">&lt;!-- 添加商品按钮 --&gt;</span><br><span class="line">&lt;el-button type&#x3D;&quot;primary&quot; class&#x3D;&quot;btnAdd&quot; @click&#x3D;&quot;add&quot;&gt;添加商品&lt;&#x2F;el-button&gt;</span><br><span class="line">&#x2F;&#x2F;编写点击事件完成商品添加</span><br><span class="line">add()&#123;</span><br><span class="line">  this.$refs.addFormRef.validate(async valid&#x3D;&gt;&#123;</span><br><span class="line">    if(!valid) return this.$message.error(&quot;请填写必要的表单项!&quot;)</span><br><span class="line"></span><br><span class="line">    &#x2F;&#x2F;将addForm进行深拷贝，避免goods_cat数组转换字符串之后导致级联选择器报错</span><br><span class="line">    const form &#x3D; _.cloneDeep(this.addForm)</span><br><span class="line">    &#x2F;&#x2F;将goods_cat从数组转换为&quot;1,2,3&quot;字符串形式</span><br><span class="line">    form.goods_cat &#x3D; form.goods_cat.join(&quot;,&quot;)</span><br><span class="line">    &#x2F;&#x2F;处理attrs数组，数组中需要包含商品的动态参数和静态属性</span><br><span class="line">    &#x2F;&#x2F;将manyTableData（动态参数）处理添加到attrs</span><br><span class="line">    this.manyTableData.forEach(item&#x3D;&gt;&#123;</span><br><span class="line">      form.attrs.push(&#123; attr_id:item.attr_id, attr_value:item.attr_vals.join(&quot; &quot;) &#125;) </span><br><span class="line">    &#125;)</span><br><span class="line">    &#x2F;&#x2F;将onlyTableData（静态属性）处理添加到attrs</span><br><span class="line">    this.onlyTableData.forEach(item&#x3D;&gt;&#123;</span><br><span class="line">      form.attrs.push(&#123; attr_id:item.attr_id, attr_value:item.attr_vals &#125;) </span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">    &#x2F;&#x2F;发送请求完成商品的添加,商品名称必须是唯一的</span><br><span class="line">    const &#123;data:res&#125; &#x3D; await this.$http.post(&#39;goods&#39;,form)</span><br><span class="line">    if(res.meta.status !&#x3D;&#x3D; 201)&#123;</span><br><span class="line">      return this.$message.error(&#39;添加商品失败&#39;)</span><br><span class="line">    &#125;</span><br><span class="line">    this.$message.success(&#39;添加商品成功&#39;)</span><br><span class="line">    &#x2F;&#x2F;编程式导航跳转到商品列表</span><br><span class="line">    this.$router.push(&#39;&#x2F;goods&#39;)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<h2 id="12-7-推送代码"><a href="#12-7-推送代码" class="headerlink" title="12.7 推送代码"></a>12.7 推送代码</h2><p>推送goods_list分支到码云<br>将代码添加到暂存区：  git add .<br>将代码提交到本地仓库： git commit -m “完成商品功能开发”<br>将代码推送到码云：  git push<br>切换到master主分支： git checkout master<br>将goods_list分支代码合并到master: git merge goods_list<br>将master推送到码云：  git push</p>
<h1 id="13-订单列表"><a href="#13-订单列表" class="headerlink" title="13.订单列表"></a>13.订单列表</h1><h2 id="13-1-创建分支"><a href="#13-1-创建分支" class="headerlink" title="13.1 创建分支"></a>13.1 创建分支</h2><p>创建order子分支并推送到码云<br>创建order子分支: git checkout -b order<br>将order分支推送到码云： git push -u origin order</p>
<h2 id="13-2-创建路由"><a href="#13-2-创建路由" class="headerlink" title="13.2 创建路由"></a>13.2 创建路由</h2><p>创建订单列表路由组件并添加路由规则</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 在components中新建order文件夹，新建Order.vue组件，组件中添加代码如下</span><br><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;h3&gt;订单列表&lt;&#x2F;h3&gt;</span><br><span class="line">        &lt;!-- 面包屑导航 --&gt;</span><br><span class="line">        &lt;el-breadcrumb separator&#x3D;&quot;&#x2F;&quot;&gt;</span><br><span class="line">            &lt;el-breadcrumb-item :to&#x3D;&quot;&#123; path: &#39;&#x2F;home&#39; &#125;&quot;&gt;首页&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">            &lt;el-breadcrumb-item&gt;订单管理&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">            &lt;el-breadcrumb-item&gt;订单列表&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">        &lt;&#x2F;el-breadcrumb&gt;</span><br><span class="line">        &lt;!-- 卡片视图区域 --&gt;</span><br><span class="line">        &lt;el-card&gt;</span><br><span class="line">            &lt;!-- 搜索栏 --&gt;</span><br><span class="line">            &lt;el-row :gutter&#x3D;&quot;20&quot;&gt;</span><br><span class="line">                &lt;el-col :span&#x3D;&quot;8&quot;&gt;</span><br><span class="line">                    &lt;el-input placeholder&#x3D;&quot;请输入内容&quot; v-model&#x3D;&quot;queryInfo.query&quot; clearable&gt;</span><br><span class="line">                        &lt;el-button slot&#x3D;&quot;append&quot; icon&#x3D;&quot;el-icon-search&quot; &gt;&lt;&#x2F;el-button&gt;</span><br><span class="line">                    &lt;&#x2F;el-input&gt;</span><br><span class="line">                &lt;&#x2F;el-col&gt;</span><br><span class="line">            &lt;&#x2F;el-row&gt;</span><br><span class="line">        &lt;&#x2F;el-card&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">        &#x2F;&#x2F; 查询条件</span><br><span class="line">        queryInfo:&#123;</span><br><span class="line">            query:&#39;&#39;,</span><br><span class="line">            pagenum:1,</span><br><span class="line">            pagesize:10</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  created() &#123;</span><br><span class="line">      </span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">      </span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style lang&#x3D;&quot;less&quot; scoped&gt;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;style&gt;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 打开router.js导入Order.vue并添加规则</span><br><span class="line">import Order from &#39;.&#x2F;components&#x2F;order&#x2F;Order.vue&#39;</span><br><span class="line"></span><br><span class="line">path: &#39;&#x2F;home&#39;, component: Home, redirect: &#39;&#x2F;welcome&#39;, children: [</span><br><span class="line">  &#123; path: &quot;&#x2F;welcome&quot;, component: Welcome &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;users&quot;, component: Users &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;rights&quot;, component: Rights &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;roles&quot;, component: Roles  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;categories&quot;, component: Cate  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;params&quot;, component: Params  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;goods&quot;, component: GoodList  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;goods&#x2F;add&quot;, component: GoodAdd  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;orders&quot;, component: Order  &#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<h2 id="13-3-实现数据展示及分页"><a href="#13-3-实现数据展示及分页" class="headerlink" title="13.3 实现数据展示及分页"></a>13.3 实现数据展示及分页</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 卡片视图区域 --&gt;</span><br><span class="line">&lt;el-card&gt;</span><br><span class="line">    &lt;!-- 搜索栏 --&gt;</span><br><span class="line">    &lt;el-row :gutter&#x3D;&quot;20&quot;&gt;</span><br><span class="line">        &lt;el-col :span&#x3D;&quot;8&quot;&gt;</span><br><span class="line">            &lt;el-input placeholder&#x3D;&quot;请输入内容&quot; v-model&#x3D;&quot;queryInfo.query&quot; clearable&gt;</span><br><span class="line">                &lt;el-button slot&#x3D;&quot;append&quot; icon&#x3D;&quot;el-icon-search&quot;&gt;&lt;&#x2F;el-button&gt;</span><br><span class="line">            &lt;&#x2F;el-input&gt;</span><br><span class="line">        &lt;&#x2F;el-col&gt;</span><br><span class="line">    &lt;&#x2F;el-row&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!-- 订单表格 --&gt;</span><br><span class="line">    &lt;el-table :data&#x3D;&quot;orderList&quot; border stripe&gt;</span><br><span class="line">        &lt;el-table-column type&#x3D;&quot;index&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">        &lt;el-table-column label&#x3D;&quot;订单编号&quot; prop&#x3D;&quot;order_number&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">        &lt;el-table-column label&#x3D;&quot;订单价格&quot; prop&#x3D;&quot;order_price&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">        &lt;el-table-column label&#x3D;&quot;是否付款&quot; prop&#x3D;&quot;pay_status&quot;&gt;</span><br><span class="line">            &lt;template slot-scope&#x3D;&quot;scope&quot;&gt;</span><br><span class="line">                &lt;el-tag type&#x3D;&quot;success&quot; v-if&#x3D;&quot;scope.row.pay_status &#x3D;&#x3D;&#x3D; &#39;1&#39;&quot;&gt;已付款&lt;&#x2F;el-tag&gt;</span><br><span class="line">                &lt;el-tag type&#x3D;&quot;danger&quot; v-else&gt;未付款&lt;&#x2F;el-tag&gt;</span><br><span class="line">            &lt;&#x2F;template&gt;</span><br><span class="line">        &lt;&#x2F;el-table-column&gt;</span><br><span class="line">        &lt;el-table-column label&#x3D;&quot;是否发货&quot; prop&#x3D;&quot;is_send&quot;&gt;&lt;&#x2F;el-table-column&gt;</span><br><span class="line">        &lt;el-table-column label&#x3D;&quot;下单时间&quot; prop&#x3D;&quot;create_time&quot;&gt;</span><br><span class="line">            &lt;template slot-scope&#x3D;&quot;scope&quot;&gt;</span><br><span class="line">                &#123;&#123;scope.row.create_time | dateFormat&#125;&#125;</span><br><span class="line">            &lt;&#x2F;template&gt;</span><br><span class="line">        &lt;&#x2F;el-table-column&gt;</span><br><span class="line">        &lt;el-table-column label&#x3D;&quot;操作&quot; width&#x3D;&quot;125px&quot;&gt;</span><br><span class="line">            &lt;template slot-scope&#x3D;&quot;scope&quot;&gt;</span><br><span class="line">                &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;primary&quot; icon&#x3D;&quot;el-icon-edit&quot;&gt;&lt;&#x2F;el-button&gt;</span><br><span class="line">                &lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;success&quot; icon&#x3D;&quot;el-icon-location&quot;&gt;&lt;&#x2F;el-button&gt;</span><br><span class="line">            &lt;&#x2F;template&gt;</span><br><span class="line">        &lt;&#x2F;el-table-column&gt;</span><br><span class="line">    &lt;&#x2F;el-table&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!-- 分页 --&gt;</span><br><span class="line">    &lt;el-pagination @size-change&#x3D;&quot;handleSizeChange&quot; @current-change&#x3D;&quot;handleCurrentChange&quot; :current-page&#x3D;&quot;queryInfo.pagenum&quot; :page-sizes&#x3D;&quot;[3, 5, 10, 15]&quot; :page-size&#x3D;&quot;queryInfo.pagesize&quot; layout&#x3D;&quot;total, sizes, prev, pager, next, jumper&quot; :total&#x3D;&quot;total&quot;&gt;</span><br><span class="line">    &lt;&#x2F;el-pagination&gt;</span><br><span class="line">&lt;&#x2F;el-card&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      &#x2F;&#x2F; 查询条件</span><br><span class="line">      queryInfo: &#123;</span><br><span class="line">        query: &#39;&#39;,</span><br><span class="line">        pagenum: 1,</span><br><span class="line">        pagesize: 10</span><br><span class="line">      &#125;,</span><br><span class="line">      &#x2F;&#x2F; 订单列表数据</span><br><span class="line">      orderList: [],</span><br><span class="line">      &#x2F;&#x2F; 数据总条数</span><br><span class="line">      total: 0</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  created() &#123;</span><br><span class="line">    this.getOrderList()</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    async getOrderList() &#123;</span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.get(&#39;orders&#39;, &#123;</span><br><span class="line">        params: this.queryInfo</span><br><span class="line">      &#125;)</span><br><span class="line"></span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">        return this.$message.error(&#39;获取订单列表数据失败!&#39;)</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      this.total &#x3D; res.data.total</span><br><span class="line">      this.orderList &#x3D; res.data.goods</span><br><span class="line">    &#125;,</span><br><span class="line">    handleSizeChange(newSize)&#123;</span><br><span class="line">        this.queryInfo.pagesize &#x3D; newSize</span><br><span class="line">        this.getOrderList()</span><br><span class="line">    &#125;,</span><br><span class="line">    handleCurrentChange(newPage)&#123;</span><br><span class="line">        this.queryInfo.pagenum &#x3D; newPage</span><br><span class="line">        this.getOrderList()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<h2 id="13-4-制作省市区县联动"><a href="#13-4-制作省市区县联动" class="headerlink" title="13.4 制作省市区县联动"></a>13.4 制作省市区县联动</h2><p>打开今天的资料，找到素材文件夹，复制citydata.js文件到components/order文件夹中<br>然后导入citydata.js文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">  import cityData from &quot;.&#x2F;citydata.js&quot;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<p>具体代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 给修改地址按钮添加点击事件</span><br><span class="line">&lt;el-button size&#x3D;&quot;mini&quot; type&#x3D;&quot;primary&quot; icon&#x3D;&quot;el-icon-edit&quot; @click&#x3D;&quot;showEditAddress&quot;&gt;&lt;&#x2F;el-button&gt;</span><br><span class="line">&#x2F;&#x2F; 添加修改地址对话框,在卡片视图下方添加</span><br><span class="line">&lt;!-- 修改地址对话框 --&gt;</span><br><span class="line">&lt;el-dialog title&#x3D;&quot;修改收货地址&quot; :visible.sync&#x3D;&quot;addressVisible&quot; width&#x3D;&quot;50%&quot; @close&#x3D;&quot;addressDialogClosed&quot;&gt;</span><br><span class="line">    &lt;!-- 添加表单 --&gt;</span><br><span class="line">    &lt;el-form :model&#x3D;&quot;addressForm&quot; :rules&#x3D;&quot;addressFormRules&quot; ref&#x3D;&quot;addressFormRef&quot; label-width&#x3D;&quot;100px&quot;&gt;</span><br><span class="line">        &lt;el-form-item label&#x3D;&quot;省市区县&quot; prop&#x3D;&quot;address1&quot;&gt;</span><br><span class="line">            &lt;el-cascader :options&#x3D;&quot;cityData&quot; v-model&#x3D;&quot;addressForm.address1&quot;&gt;&lt;&#x2F;el-cascader&gt;</span><br><span class="line">        &lt;&#x2F;el-form-item&gt;</span><br><span class="line">        &lt;el-form-item label&#x3D;&quot;详细地址&quot; prop&#x3D;&quot;address2&quot;&gt;</span><br><span class="line">            &lt;el-input v-model&#x3D;&quot;addressForm.address2&quot;&gt;&lt;&#x2F;el-input&gt;</span><br><span class="line">        &lt;&#x2F;el-form-item&gt;</span><br><span class="line">    &lt;&#x2F;el-form&gt;</span><br><span class="line">    &lt;span slot&#x3D;&quot;footer&quot; class&#x3D;&quot;dialog-footer&quot;&gt;</span><br><span class="line">        &lt;el-button @click&#x3D;&quot;addressVisible &#x3D; false&quot;&gt;取 消&lt;&#x2F;el-button&gt;</span><br><span class="line">        &lt;el-button type&#x3D;&quot;primary&quot; @click&#x3D;&quot;addressVisible &#x3D; false&quot;&gt;确 定&lt;&#x2F;el-button&gt;</span><br><span class="line">    &lt;&#x2F;span&gt;</span><br><span class="line">&lt;&#x2F;el-dialog&gt;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; js部分的代码</span><br><span class="line">&lt;script&gt;</span><br><span class="line">import cityData from &quot;.&#x2F;citydata.js&quot;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      ......</span><br><span class="line">      &#x2F;&#x2F; 控制修改地址对话框的显示和隐藏</span><br><span class="line">      addressVisible:false,</span><br><span class="line">      &#x2F;&#x2F; 修改收货地址的表单</span><br><span class="line">      addressForm:&#123;</span><br><span class="line">          address1:[],</span><br><span class="line">          address2:&#39;&#39;</span><br><span class="line">      &#125;,</span><br><span class="line">      addressFormRules:&#123;</span><br><span class="line">          address1:[&#123; required: true, message: &#39;请选择省市区县&#39;, trigger: &#39;blur&#39; &#125;],</span><br><span class="line">          address2:[&#123; required: true, message: &#39;请输入详细地址&#39;, trigger: &#39;blur&#39; &#125;],</span><br><span class="line">      &#125;,</span><br><span class="line">      &#x2F;&#x2F; 将导入的cityData数据保存起来</span><br><span class="line">      cityData:cityData</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;,methods: &#123;</span><br><span class="line">    ......</span><br><span class="line">    showEditAddress() &#123;</span><br><span class="line">      &#x2F;&#x2F; 当用户点击修改收货地址按钮时触发</span><br><span class="line">      this.addressVisible &#x3D; true;</span><br><span class="line">    &#125;,</span><br><span class="line">    addressDialogClosed()&#123;</span><br><span class="line">        this.$refs.addressFormRef.resetFields()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line">&lt;style lang&#x3D;&quot;less&quot; scoped&gt;</span><br><span class="line">.el-cascader&#123;</span><br><span class="line">    width: 100%;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;style&gt;</span><br></pre></td></tr></table></figure>

<h2 id="13-5-制作物流进度对话框"><a href="#13-5-制作物流进度对话框" class="headerlink" title="13.5 制作物流进度对话框"></a>13.5 制作物流进度对话框</h2><p>因为我们使用的是element-ui中提供的Timeline组件，所以需要导入并注册组件<br>打开element.js,编写代码会进行导入和注册</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">import &#123;</span><br><span class="line">    Timeline,TimelineItem</span><br><span class="line">&#125; from &#39;element-ui&#39;</span><br><span class="line"></span><br><span class="line">Vue.use(Timeline)</span><br><span class="line">Vue.use(TimelineItem)</span><br></pre></td></tr></table></figure>

<p>打开Order.vue文件，添加代码实现物流进度对话框</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 物流信息进度对话框 --&gt;</span><br><span class="line">&lt;el-dialog title&#x3D;&quot;物流进度&quot; :visible.sync&#x3D;&quot;progressVisible&quot; width&#x3D;&quot;50%&quot;&gt;</span><br><span class="line">    &lt;!-- 时间线组件  --&gt;</span><br><span class="line">    &lt;el-timeline&gt;</span><br><span class="line">        &lt;el-timeline-item v-for&#x3D;&quot;(activity, index) in progressInfo&quot; </span><br><span class="line">        :key&#x3D;&quot;index&quot; :timestamp&#x3D;&quot;activity.time&quot;&gt;</span><br><span class="line">            &#123;&#123;activity.context&#125;&#125;</span><br><span class="line">        &lt;&#x2F;el-timeline-item&gt;</span><br><span class="line">    &lt;&#x2F;el-timeline&gt;</span><br><span class="line">&lt;&#x2F;el-dialog&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">import cityData from &#39;.&#x2F;citydata.js&#39;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      ......</span><br><span class="line">      &#x2F;&#x2F;控制物流进度对话框的显示和隐藏</span><br><span class="line">      progressVisible: false,</span><br><span class="line">      &#x2F;&#x2F;保存物流信息</span><br><span class="line">      progressInfo: []</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;,methods: &#123;</span><br><span class="line">    ......</span><br><span class="line">    async showProgress() &#123;</span><br><span class="line">      &#x2F;&#x2F;发送请求获取物流数据</span><br><span class="line">      const &#123; data: res &#125; &#x3D; await this.$http.get(&#39;&#x2F;kuaidi&#x2F;804909574412544580&#39;)</span><br><span class="line"></span><br><span class="line">      if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">        return this.$message.error(&#39;获取物流进度失败!&#39;)</span><br><span class="line">      &#125;</span><br><span class="line">      this.progressInfo &#x3D; res.data</span><br><span class="line">      &#x2F;&#x2F;显示对话框</span><br><span class="line">      this.progressVisible &#x3D; true</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<h2 id="13-6-推送代码"><a href="#13-6-推送代码" class="headerlink" title="13.6 推送代码"></a>13.6 推送代码</h2><p>将order分支代码推送至码云<br>将代码添加到暂存区：  git add .<br>将代码提交到本地仓库： git commit -m “完成订单列表功能开发”<br>将代码推送到码云：  git push<br>切换到master主分支： git checkout master<br>将goods_list分支代码合并到master: git merge order<br>将master推送到码云：  git push</p>
<h1 id="14-数据统计"><a href="#14-数据统计" class="headerlink" title="14.数据统计"></a>14.数据统计</h1><h2 id="14-1-创建分支"><a href="#14-1-创建分支" class="headerlink" title="14.1 创建分支"></a>14.1 创建分支</h2><p>创建report子分支并推送到码云<br>创建report子分支: git checkout -b report<br>将report分支推送到码云： git push -u origin report</p>
<h2 id="14-2-创建路由"><a href="#14-2-创建路由" class="headerlink" title="14.2 创建路由"></a>14.2 创建路由</h2><p>创建数据统计路由组件并添加路由规则</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;在components中新建report文件夹，新建Report.vue组件，组件中添加代码如下</span><br><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;h3&gt;数据报表&lt;&#x2F;h3&gt;</span><br><span class="line">        &lt;!-- 面包屑导航 --&gt;</span><br><span class="line">        &lt;el-breadcrumb separator&#x3D;&quot;&#x2F;&quot;&gt;</span><br><span class="line">            &lt;el-breadcrumb-item :to&#x3D;&quot;&#123; path: &#39;&#x2F;home&#39; &#125;&quot;&gt;首页&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">            &lt;el-breadcrumb-item&gt;数据统计&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">            &lt;el-breadcrumb-item&gt;数据报表&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">        &lt;&#x2F;el-breadcrumb&gt;</span><br><span class="line">        &lt;!-- 卡片视图区域 --&gt;</span><br><span class="line">        &lt;el-card&gt;&lt;&#x2F;el-card&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line">    </span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,created()&#123;</span><br><span class="line"></span><br><span class="line">  &#125;,methods:&#123;</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style lang&#x3D;&quot;less&quot; scoped&gt;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;style&gt;</span><br></pre></td></tr></table></figure>

<p>打开router.js，导入Report.vue并设置路由规则</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">import Report from &#39;.&#x2F;components&#x2F;report&#x2F;Report.vue&#39;</span><br><span class="line">path: &#39;&#x2F;home&#39;, component: Home, redirect: &#39;&#x2F;welcome&#39;, children: [</span><br><span class="line">  &#123; path: &quot;&#x2F;welcome&quot;, component: Welcome &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;users&quot;, component: Users &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;rights&quot;, component: Rights &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;roles&quot;, component: Roles  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;categories&quot;, component: Cate  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;params&quot;, component: Params  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;goods&quot;, component: GoodList  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;goods&#x2F;add&quot;, component: GoodAdd  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;orders&quot;, component: Order  &#125;,</span><br><span class="line">  &#123; path: &quot;&#x2F;reports&quot;, component: Report  &#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<h2 id="14-3-导入ECharts并使用"><a href="#14-3-导入ECharts并使用" class="headerlink" title="14.3 导入ECharts并使用"></a>14.3 导入ECharts并使用</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;h3&gt;数据报表&lt;&#x2F;h3&gt;</span><br><span class="line">        &lt;!-- 面包屑导航 --&gt;</span><br><span class="line">        &lt;el-breadcrumb separator&#x3D;&quot;&#x2F;&quot;&gt;</span><br><span class="line">            &lt;el-breadcrumb-item :to&#x3D;&quot;&#123; path: &#39;&#x2F;home&#39; &#125;&quot;&gt;首页&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">            &lt;el-breadcrumb-item&gt;数据统计&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">            &lt;el-breadcrumb-item&gt;数据报表&lt;&#x2F;el-breadcrumb-item&gt;</span><br><span class="line">        &lt;&#x2F;el-breadcrumb&gt;</span><br><span class="line">        &lt;!-- 卡片视图区域 --&gt;</span><br><span class="line">        &lt;el-card&gt;</span><br><span class="line">            &lt;div id&#x3D;&quot;main&quot; style&#x3D;&quot;width:750px;height:400px;&quot;&gt;&lt;&#x2F;div&gt;</span><br><span class="line">        &lt;&#x2F;el-card&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line">    </span><br><span class="line">&lt;script&gt;</span><br><span class="line">&#x2F;&#x2F;导入echarts</span><br><span class="line">import echarts from &#39;echarts&#39;</span><br><span class="line">&#x2F;&#x2F;导入lodash</span><br><span class="line">import _ from &#39;lodash&#39;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      &#x2F;&#x2F;需要跟请求的折线图数据合并的options</span><br><span class="line">      options: &#123;</span><br><span class="line">        title: &#123;</span><br><span class="line">          text: &#39;用户来源&#39;</span><br><span class="line">        &#125;,</span><br><span class="line">        tooltip: &#123;</span><br><span class="line">          trigger: &#39;axis&#39;,</span><br><span class="line">          axisPointer: &#123;</span><br><span class="line">            type: &#39;cross&#39;,</span><br><span class="line">            label: &#123;</span><br><span class="line">              backgroundColor: &#39;#E9EEF3&#39;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        grid: &#123;</span><br><span class="line">          left: &#39;3%&#39;,</span><br><span class="line">          right: &#39;4%&#39;,</span><br><span class="line">          bottom: &#39;3%&#39;,</span><br><span class="line">          containLabel: true</span><br><span class="line">        &#125;,</span><br><span class="line">        xAxis: [</span><br><span class="line">          &#123;</span><br><span class="line">            boundaryGap: false</span><br><span class="line">          &#125;</span><br><span class="line">        ],</span><br><span class="line">        yAxis: [</span><br><span class="line">          &#123;</span><br><span class="line">            type: &#39;value&#39;</span><br><span class="line">          &#125;</span><br><span class="line">        ]</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  created() &#123;&#125;,</span><br><span class="line">  async mounted() &#123;</span><br><span class="line">    &#x2F;&#x2F; 在页面dom元素加载完毕之后执行的钩子函数mounted</span><br><span class="line">    &#x2F;&#x2F; 基于准备好的dom，初始化echarts实例</span><br><span class="line">    var myChart &#x3D; echarts.init(document.getElementById(&#39;main&#39;))</span><br><span class="line">    &#x2F;&#x2F;准备数据和配置项</span><br><span class="line">    &#x2F;&#x2F;发送请求获取折线图数据</span><br><span class="line">    const &#123; data: res &#125; &#x3D; await this.$http.get(&#39;reports&#x2F;type&#x2F;1&#39;)</span><br><span class="line"></span><br><span class="line">    if (res.meta.status !&#x3D;&#x3D; 200) &#123;</span><br><span class="line">      return this.$message.error(&#39;获取折线图数据失败&#39;)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    &#x2F;&#x2F;合并res.data和this.options</span><br><span class="line">    const result &#x3D; _.merge(res.data,this.options)</span><br><span class="line"></span><br><span class="line">    &#x2F;&#x2F; 使用获取的数据展示图表</span><br><span class="line">    myChart.setOption(result)</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style lang&#x3D;&quot;less&quot; scoped&gt;</span><br><span class="line">&lt;&#x2F;style&gt;</span><br></pre></td></tr></table></figure>

<h2 id="14-4-推送代码"><a href="#14-4-推送代码" class="headerlink" title="14.4 推送代码"></a>14.4 推送代码</h2><p>推送report分支到码云<br>将代码添加到暂存区：  git add .<br>将代码提交到本地仓库： git commit -m “完成数据报表功能开发”<br>将代码推送到码云：  git push<br>切换到master主分支： git checkout master<br>将report分支代码合并到master: git merge report<br>将master推送到码云：  git push</p>
<h1 id="15-项目优化"><a href="#15-项目优化" class="headerlink" title="15.项目优化"></a>15.项目优化</h1><h2 id="15-1-添加进度条"><a href="#15-1-添加进度条" class="headerlink" title="15.1 添加进度条"></a>15.1 添加进度条</h2><p>给项目添加进度条效果，先打开项目控制台，打开依赖，安装nprogress<br>打开main.js，编写如下代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 导入进度条插件</span><br><span class="line">import NProgress from &#39;nprogress&#39;</span><br><span class="line">&#x2F;&#x2F; 导入进度条样式</span><br><span class="line">import &#39;nprogress&#x2F;nprogress.css&#39;</span><br><span class="line">.....</span><br><span class="line">&#x2F;&#x2F; 请求在到达服务器之前，先会调用use中的这个回调函数来添加请求头信息</span><br><span class="line">axios.interceptors.request.use(config &#x3D;&gt; &#123;</span><br><span class="line">  &#x2F;&#x2F; 当进入request拦截器，表示发送了请求，我们就开启进度条</span><br><span class="line">  NProgress.start()</span><br><span class="line">  &#x2F;&#x2F; 为请求头对象，添加token验证的Authorization字段</span><br><span class="line">  config.headers.Authorization &#x3D; window.sessionStorage.getItem(&quot;token&quot;)</span><br><span class="line">  &#x2F;&#x2F; 必须返回config</span><br><span class="line">  return config</span><br><span class="line">&#125;)</span><br><span class="line">&#x2F;&#x2F; 在response拦截器中，隐藏进度条</span><br><span class="line">axios.interceptors.response.use(config &#x3D;&gt;&#123;</span><br><span class="line">  &#x2F;&#x2F; 当进入response拦截器，表示请求已经结束，我们就结束进度条</span><br><span class="line">  NProgress.done()</span><br><span class="line">  return config</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="15-2-根据报错修改代码"><a href="#15-2-根据报错修改代码" class="headerlink" title="15.2 根据报错修改代码"></a>15.2 根据报错修改代码</h2><p>根据ESLint的警告提示更改对应的代码<br>在.prettierrc文件中更改设置”printWidth”:200,  将每行代码的文字数量更改为200</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    &quot;semi&quot;:false,</span><br><span class="line">    &quot;singleQuote&quot;:true,</span><br><span class="line">    &quot;printWidth&quot;:200</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="15-3-执行build"><a href="#15-3-执行build" class="headerlink" title="15.3 执行build"></a>15.3 执行build</h2><p>安装一个插件（babel-plugin-transform-remove-console）在项目build阶段移除所有的console信息<br>打开项目控制台，点击依赖-&gt;开发依赖，输入babel-plugin-transform-remove-console，安装<br>打开babel.config.js，编辑代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 项目发布阶段需要用到的babel插件</span><br><span class="line">const productPlugins &#x3D; []</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 判断是开发还是发布阶段</span><br><span class="line">if(process.env.NODE_ENV &#x3D;&#x3D;&#x3D; &#39;production&#39;)&#123;</span><br><span class="line">  &#x2F;&#x2F; 发布阶段</span><br><span class="line">  productPlugins.push(&quot;transform-remove-console&quot;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">module.exports &#x3D; &#123;</span><br><span class="line">  &quot;presets&quot;: [</span><br><span class="line">    &quot;@vue&#x2F;app&quot;</span><br><span class="line">  ],</span><br><span class="line">  &quot;plugins&quot;: [</span><br><span class="line">    [</span><br><span class="line">      &quot;component&quot;,</span><br><span class="line">      &#123;</span><br><span class="line">        &quot;libraryName&quot;: &quot;element-ui&quot;,</span><br><span class="line">        &quot;styleLibraryName&quot;: &quot;theme-chalk&quot;</span><br><span class="line">      &#125;</span><br><span class="line">    ],</span><br><span class="line">    ...productPlugins</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="15-4-生成打包报告"><a href="#15-4-生成打包报告" class="headerlink" title="15.4 生成打包报告"></a>15.4 生成打包报告</h2><p>A.命令行形式生成打包报告<br>vue-cli-service build –report<br>B.在vue控制台生成打包报告<br>点击“任务”=&gt;“build”=&gt;“运行”<br>运行完毕之后点击右侧“分析”，“控制台”面板查看报告</p>
<h2 id="15-5-修改webpack的默认配置"><a href="#15-5-修改webpack的默认配置" class="headerlink" title="15.5 修改webpack的默认配置"></a>15.5 修改webpack的默认配置</h2><p>默认情况下，vue-cli 3.0生成的项目，隐藏了webpack配置项，如果我们需要配置webpack<br>需要通过vue.config.js来配置。<br>在项目根目录中创建vue.config.js文件，</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">module.exports &#x3D; &#123;</span><br><span class="line">    chainWebpack:config&#x3D;&gt;&#123;</span><br><span class="line">        &#x2F;&#x2F; 发布模式</span><br><span class="line">        config.when(process.env.NODE_ENV &#x3D;&#x3D;&#x3D; &#39;production&#39;,config&#x3D;&gt;&#123;</span><br><span class="line">            &#x2F;&#x2F; entry找到默认的打包入口，调用clear则是删除默认的打包入口</span><br><span class="line">            &#x2F;&#x2F; add添加新的打包入口</span><br><span class="line">            config.entry(&#39;app&#39;).clear().add(&#39;.&#x2F;src&#x2F;main-prod.js&#39;)</span><br><span class="line">        &#125;)</span><br><span class="line">        &#x2F;&#x2F; 开发模式</span><br><span class="line">        config.when(process.env.NODE_ENV &#x3D;&#x3D;&#x3D; &#39;development&#39;,config&#x3D;&gt;&#123;</span><br><span class="line">            config.entry(&#39;app&#39;).clear().add(&#39;.&#x2F;src&#x2F;main-dev.js&#39;)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>补充：<br>chainWebpack可以通过链式编程的形式，修改webpack配置<br>configureWebpack可以通过操作对象的形式，修改webpack配置</p>
<h2 id="15-6-加载外部CDN"><a href="#15-6-加载外部CDN" class="headerlink" title="15.6 加载外部CDN"></a>15.6 加载外部CDN</h2><p>默认情况下，依赖项的所有第三方包都会被打包到js/chunk-vendors.<strong>**</strong>.js文件中，导致该js文件过大<br>那么我们可以通过externals排除这些包，使它们不被打包到js/chunk-vendors.<strong>**</strong>.js文件中</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">module.exports &#x3D; &#123;</span><br><span class="line">    chainWebpack:config&#x3D;&gt;&#123;</span><br><span class="line">        &#x2F;&#x2F; 发布模式</span><br><span class="line">        config.when(process.env.NODE_ENV &#x3D;&#x3D;&#x3D; &#39;production&#39;,config&#x3D;&gt;&#123;</span><br><span class="line">            &#x2F;&#x2F; entry找到默认的打包入口，调用clear则是删除默认的打包入口</span><br><span class="line">            &#x2F;&#x2F; add添加新的打包入口</span><br><span class="line">            config.entry(&#39;app&#39;).clear().add(&#39;.&#x2F;src&#x2F;main-prod.js&#39;)</span><br><span class="line"></span><br><span class="line">            &#x2F;&#x2F; 使用externals设置排除项</span><br><span class="line">            config.set(&#39;externals&#39;,&#123;</span><br><span class="line">                vue:&#39;Vue&#39;,</span><br><span class="line">                &#39;vue-router&#39;:&#39;VueRouter&#39;,</span><br><span class="line">                axios:&#39;axios&#39;,</span><br><span class="line">                lodash:&#39;_&#39;,</span><br><span class="line">                echarts:&#39;echarts&#39;,</span><br><span class="line">                nprogress:&#39;NProgress&#39;,</span><br><span class="line">                &#39;vue-quill-editor&#39;:&#39;VueQuillEditor&#39;</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;)</span><br><span class="line">        &#x2F;&#x2F; 开发模式</span><br><span class="line">        config.when(process.env.NODE_ENV &#x3D;&#x3D;&#x3D; &#39;development&#39;,config&#x3D;&gt;&#123;</span><br><span class="line">            config.entry(&#39;app&#39;).clear().add(&#39;.&#x2F;src&#x2F;main-dev.js&#39;)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>设置好排除之后，为了使我们可以使用vue，axios等内容，我们需要加载外部CDN的形式解决引入依赖项。<br>打开开发入口文件main-prod.js,删除掉默认的引入代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line">import Vue from &#39;vue&#39;</span><br><span class="line">import App from &#39;.&#x2F;App.vue&#39;</span><br><span class="line">import router from &#39;.&#x2F;router&#39;</span><br><span class="line">&#x2F;&#x2F; import &#39;.&#x2F;plugins&#x2F;element.js&#39;</span><br><span class="line">&#x2F;&#x2F; 导入字体图标</span><br><span class="line">import &#39;.&#x2F;assets&#x2F;fonts&#x2F;iconfont.css&#39;</span><br><span class="line">&#x2F;&#x2F; 导入全局样式</span><br><span class="line">import &#39;.&#x2F;assets&#x2F;css&#x2F;global.css&#39;</span><br><span class="line">&#x2F;&#x2F; 导入第三方组件vue-table-with-tree-grid</span><br><span class="line">import TreeTable from &#39;vue-table-with-tree-grid&#39;</span><br><span class="line">&#x2F;&#x2F; 导入进度条插件</span><br><span class="line">import NProgress from &#39;nprogress&#39;</span><br><span class="line">&#x2F;&#x2F; 导入进度条样式</span><br><span class="line">&#x2F;&#x2F; import &#39;nprogress&#x2F;nprogress.css&#39;</span><br><span class="line">&#x2F;&#x2F; 导入axios</span><br><span class="line">import axios from &#39;axios&#39;</span><br><span class="line">&#x2F;&#x2F; 导入vue-quill-editor（富文本编辑器）</span><br><span class="line">import VueQuillEditor from &#39;vue-quill-editor&#39;</span><br><span class="line">&#x2F;&#x2F; 导入vue-quill-editor的样式</span><br><span class="line">&#x2F;&#x2F; import &#39;quill&#x2F;dist&#x2F;quill.core.css&#39;</span><br><span class="line">&#x2F;&#x2F; import &#39;quill&#x2F;dist&#x2F;quill.snow.css&#39;</span><br><span class="line">&#x2F;&#x2F; import &#39;quill&#x2F;dist&#x2F;quill.bubble.css&#39;</span><br><span class="line"></span><br><span class="line">axios.defaults.baseURL &#x3D; &#39;http:&#x2F;&#x2F;127.0.0.1:8888&#x2F;api&#x2F;private&#x2F;v1&#x2F;&#39;</span><br><span class="line">&#x2F;&#x2F;请求在到达服务器之前，先会调用use中的这个回调函数来添加请求头信息</span><br><span class="line">axios.interceptors.request.use(config &#x3D;&gt; &#123;</span><br><span class="line">  &#x2F;&#x2F;当进入request拦截器，表示发送了请求，我们就开启进度条</span><br><span class="line">  NProgress.start()</span><br><span class="line">  &#x2F;&#x2F;为请求头对象，添加token验证的Authorization字段</span><br><span class="line">  config.headers.Authorization &#x3D; window.sessionStorage.getItem(&quot;token&quot;)</span><br><span class="line">  &#x2F;&#x2F;必须返回config</span><br><span class="line">  return config</span><br><span class="line">&#125;)</span><br><span class="line">&#x2F;&#x2F;在response拦截器中，隐藏进度条</span><br><span class="line">axios.interceptors.response.use(config &#x3D;&gt;&#123;</span><br><span class="line">  &#x2F;&#x2F;当进入response拦截器，表示请求已经结束，我们就结束进度条</span><br><span class="line">  NProgress.done()</span><br><span class="line">  return config</span><br><span class="line">&#125;)</span><br><span class="line">Vue.prototype.$http &#x3D; axios</span><br><span class="line"></span><br><span class="line">Vue.config.productionTip &#x3D; false</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 全局注册组件</span><br><span class="line">Vue.component(&#39;tree-table&#39;, TreeTable)</span><br><span class="line">&#x2F;&#x2F; 全局注册富文本组件</span><br><span class="line">Vue.use(VueQuillEditor)</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;创建过滤器将秒数过滤为年月日，时分秒</span><br><span class="line">Vue.filter(&#39;dateFormat&#39;,function(originVal)&#123;</span><br><span class="line">  const dt &#x3D; new Date(originVal)</span><br><span class="line">  const y &#x3D; dt.getFullYear()</span><br><span class="line">  const m &#x3D; (dt.getMonth()+1+&#39;&#39;).padStart(2,&#39;0&#39;)</span><br><span class="line">  const d &#x3D; (dt.getDate()+&#39;&#39;).padStart(2,&#39;0&#39;)</span><br><span class="line"></span><br><span class="line">  const hh &#x3D; (dt.getHours()+&#39;&#39;).padStart(2,&#39;0&#39;)</span><br><span class="line">  const mm &#x3D; (dt.getMinutes()+&#39;&#39;).padStart(2,&#39;0&#39;)</span><br><span class="line">  const ss &#x3D; (dt.getSeconds()+&#39;&#39;).padStart(2,&#39;0&#39;)</span><br><span class="line"></span><br><span class="line">  return &#96;$&#123;y&#125;-$&#123;m&#125;-$&#123;d&#125; $&#123;hh&#125;:$&#123;mm&#125;:$&#123;ss&#125;&#96;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">new Vue(&#123;</span><br><span class="line">  router,</span><br><span class="line">  render: h &#x3D;&gt; h(App)</span><br><span class="line">&#125;).$mount(&#39;#app&#39;)</span><br></pre></td></tr></table></figure>

<p>然后打开public/index.html添加外部cdn引入代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang&#x3D;&quot;en&quot;&gt;</span><br><span class="line">  &lt;head&gt;</span><br><span class="line">    &lt;meta charset&#x3D;&quot;utf-8&quot;&gt;</span><br><span class="line">    &lt;meta http-equiv&#x3D;&quot;X-UA-Compatible&quot; content&#x3D;&quot;IE&#x3D;edge&quot;&gt;</span><br><span class="line">    &lt;meta name&#x3D;&quot;viewport&quot; content&#x3D;&quot;width&#x3D;device-width,initial-scale&#x3D;1.0&quot;&gt;</span><br><span class="line">    &lt;link rel&#x3D;&quot;icon&quot; href&#x3D;&quot;&lt;%&#x3D; BASE_URL %&gt;favicon.ico&quot;&gt;</span><br><span class="line">    &lt;title&gt;电商后台管理系统&lt;&#x2F;title&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!-- nprogress 的样式表文件 --&gt;</span><br><span class="line">    &lt;link rel&#x3D;&quot;stylesheet&quot; href&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;nprogress&#x2F;0.2.0&#x2F;nprogress.min.css&quot; &#x2F;&gt;</span><br><span class="line">    &lt;!-- 富文本编辑器 的样式表文件 --&gt;</span><br><span class="line">    &lt;link rel&#x3D;&quot;stylesheet&quot; href&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;quill&#x2F;1.3.4&#x2F;quill.core.min.css&quot; &#x2F;&gt;</span><br><span class="line">    &lt;link rel&#x3D;&quot;stylesheet&quot; href&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;quill&#x2F;1.3.4&#x2F;quill.snow.min.css&quot; &#x2F;&gt;</span><br><span class="line">    &lt;link rel&#x3D;&quot;stylesheet&quot; href&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;quill&#x2F;1.3.4&#x2F;quill.bubble.min.css&quot; &#x2F;&gt;</span><br><span class="line">    &lt;!-- element-ui 的样式表文件 --&gt;</span><br><span class="line">    &lt;link rel&#x3D;&quot;stylesheet&quot; href&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;element-ui&#x2F;2.8.2&#x2F;theme-chalk&#x2F;index.css&quot; &#x2F;&gt;</span><br><span class="line"></span><br><span class="line">    &lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;vue&#x2F;2.5.22&#x2F;vue.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">    &lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;vue-router&#x2F;3.0.1&#x2F;vue-router.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">    &lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;axios&#x2F;0.18.0&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">    &lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;lodash.js&#x2F;4.17.11&#x2F;lodash.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">    &lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;echarts&#x2F;4.1.0&#x2F;echarts.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">    &lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;nprogress&#x2F;0.2.0&#x2F;nprogress.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">    &lt;!-- 富文本编辑器的 js 文件 --&gt;</span><br><span class="line">    &lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;quill&#x2F;1.3.4&#x2F;quill.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">    &lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.jsdelivr.net&#x2F;npm&#x2F;vue-quill-editor@3.0.4&#x2F;dist&#x2F;vue-quill-editor.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!-- element-ui 的 js 文件 --&gt;</span><br><span class="line">    &lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;element-ui&#x2F;2.8.2&#x2F;index.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">  &lt;&#x2F;head&gt;</span><br><span class="line">  &lt;body&gt;</span><br><span class="line">    &lt;noscript&gt;</span><br><span class="line">      &lt;strong&gt;We&#39;re sorry but vue_shop doesn&#39;t work properly without JavaScript enabled. Please enable it to continue.&lt;&#x2F;strong&gt;</span><br><span class="line">    &lt;&#x2F;noscript&gt;</span><br><span class="line">    &lt;div id&#x3D;&quot;app&quot;&gt;&lt;&#x2F;div&gt;</span><br><span class="line">    &lt;!-- built files will be auto injected --&gt;</span><br><span class="line">  &lt;&#x2F;body&gt;</span><br><span class="line">&lt;&#x2F;html&gt;</span><br></pre></td></tr></table></figure>

<h2 id="15-7-定制首页内容"><a href="#15-7-定制首页内容" class="headerlink" title="15.7 定制首页内容"></a>15.7 定制首页内容</h2><p>开发环境的首页和发布环境的首页展示内容的形式有所不同<br>如开发环境中使用的是import加载第三方包，而发布环境则是使用CDN，那么首页也需根据环境不同来进行不同的实现<br>我们可以通过插件的方式来定制首页内容，打开vue.config.js，编写代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">module.exports &#x3D; &#123;</span><br><span class="line">    chainWebpack:config&#x3D;&gt;&#123;</span><br><span class="line">        config.when(process.env.NODE_ENV &#x3D;&#x3D;&#x3D; &#39;production&#39;,config&#x3D;&gt;&#123;</span><br><span class="line">            ......</span><br><span class="line">            </span><br><span class="line">            &#x2F;&#x2F; 使用插件</span><br><span class="line">            config.plugin(&#39;html&#39;).tap(args&#x3D;&gt;&#123;</span><br><span class="line">                &#x2F;&#x2F; 添加参数isProd</span><br><span class="line">                args[0].isProd &#x3D; true</span><br><span class="line">                return args</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;)</span><br><span class="line"></span><br><span class="line">        config.when(process.env.NODE_ENV &#x3D;&#x3D;&#x3D; &#39;development&#39;,config&#x3D;&gt;&#123;</span><br><span class="line">            config.entry(&#39;app&#39;).clear().add(&#39;.&#x2F;src&#x2F;main-dev.js&#39;)</span><br><span class="line"></span><br><span class="line">            &#x2F;&#x2F; 使用插件</span><br><span class="line">            config.plugin(&#39;html&#39;).tap(args&#x3D;&gt;&#123;</span><br><span class="line">                &#x2F;&#x2F; 添加参数isProd</span><br><span class="line">                args[0].isProd &#x3D; false</span><br><span class="line">                return args</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后在public/index.html中使用插件判断是否为发布环境并定制首页内容</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang&#x3D;&quot;en&quot;&gt;</span><br><span class="line">  &lt;head&gt;</span><br><span class="line">    &lt;meta charset&#x3D;&quot;utf-8&quot;&gt;</span><br><span class="line">    &lt;meta http-equiv&#x3D;&quot;X-UA-Compatible&quot; content&#x3D;&quot;IE&#x3D;edge&quot;&gt;</span><br><span class="line">    &lt;meta name&#x3D;&quot;viewport&quot; content&#x3D;&quot;width&#x3D;device-width,initial-scale&#x3D;1.0&quot;&gt;</span><br><span class="line">    &lt;link rel&#x3D;&quot;icon&quot; href&#x3D;&quot;&lt;%&#x3D; BASE_URL %&gt;favicon.ico&quot;&gt;</span><br><span class="line">    &lt;title&gt;&lt;%&#x3D; htmlWebpackPlugin.options.isProd ? &#39;&#39; : &#39;dev - &#39; %&gt;电商后台管理系统&lt;&#x2F;title&gt;</span><br><span class="line"></span><br><span class="line">    &lt;% if(htmlWebpackPlugin.options.isProd)&#123; %&gt;</span><br><span class="line">    &lt;!-- nprogress 的样式表文件 --&gt;</span><br><span class="line">    &lt;link rel&#x3D;&quot;stylesheet&quot; href&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;nprogress&#x2F;0.2.0&#x2F;nprogress.min.css&quot; &#x2F;&gt;</span><br><span class="line">    ........</span><br><span class="line">    &lt;!-- element-ui 的 js 文件 --&gt;</span><br><span class="line">    &lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;element-ui&#x2F;2.8.2&#x2F;index.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">    &lt;% &#125; %&gt;</span><br><span class="line">  &lt;&#x2F;head&gt;</span><br><span class="line">  .......</span><br></pre></td></tr></table></figure>

<h2 id="15-8-路由懒加载"><a href="#15-8-路由懒加载" class="headerlink" title="15.8 路由懒加载"></a>15.8 路由懒加载</h2><p>当路由被访问时才加载对应的路由文件，就是路由懒加载。<br>路由懒加载实现步骤：<br>1.安装 @babel/plugin-syntax-dynamic-import<br>打开vue控制台，点击依赖-&gt;安装依赖-&gt;开发依赖-&gt;搜索@babel/plugin-syntax-dynamic-import<br>点击安装。</p>
<p>2.在babel.config.js中声明该插件，打开babel.config.js</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 项目发布阶段需要用到的babel插件</span><br><span class="line">const productPlugins &#x3D; []</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 判断是开发还是发布阶段</span><br><span class="line">if(process.env.NODE_ENV &#x3D;&#x3D;&#x3D; &#39;production&#39;)&#123;</span><br><span class="line">  &#x2F;&#x2F; 发布阶段</span><br><span class="line">  productPlugins.push(&quot;transform-remove-console&quot;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">module.exports &#x3D; &#123;</span><br><span class="line">  &quot;presets&quot;: [</span><br><span class="line">    &quot;@vue&#x2F;app&quot;</span><br><span class="line">  ],</span><br><span class="line">  &quot;plugins&quot;: [</span><br><span class="line">    [</span><br><span class="line">      &quot;component&quot;,</span><br><span class="line">      &#123;</span><br><span class="line">        &quot;libraryName&quot;: &quot;element-ui&quot;,</span><br><span class="line">        &quot;styleLibraryName&quot;: &quot;theme-chalk&quot;</span><br><span class="line">      &#125;</span><br><span class="line">    ],</span><br><span class="line">    ...productPlugins,</span><br><span class="line">    &#x2F;&#x2F; 配置路由懒加载插件</span><br><span class="line">    &quot;@babel&#x2F;plugin-syntax-dynamic-import&quot;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>3.将路由更改为按需加载的形式，打开router.js，更改引入组件代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">import Vue from &#39;vue&#39;</span><br><span class="line">import Router from &#39;vue-router&#39;</span><br><span class="line">const Login &#x3D; () &#x3D;&gt; import(&#x2F;* webpackChunkName:&quot;login_home_welcome&quot; *&#x2F; &#39;.&#x2F;components&#x2F;Login.vue&#39;)</span><br><span class="line">&#x2F;&#x2F; import Login from &#39;.&#x2F;components&#x2F;Login.vue&#39;</span><br><span class="line">const Home &#x3D; () &#x3D;&gt; import(&#x2F;* webpackChunkName:&quot;login_home_welcome&quot; *&#x2F; &#39;.&#x2F;components&#x2F;Home.vue&#39;)</span><br><span class="line">&#x2F;&#x2F; import Home from &#39;.&#x2F;components&#x2F;Home.vue&#39;</span><br><span class="line">const Welcome &#x3D; () &#x3D;&gt; import(&#x2F;* webpackChunkName:&quot;login_home_welcome&quot; *&#x2F; &#39;.&#x2F;components&#x2F;Welcome.vue&#39;)</span><br><span class="line">&#x2F;&#x2F; import Welcome from &#39;.&#x2F;components&#x2F;Welcome.vue&#39;</span><br><span class="line">const Users &#x3D; () &#x3D;&gt; import(&#x2F;* webpackChunkName:&quot;user&quot; *&#x2F; &#39;.&#x2F;components&#x2F;user&#x2F;Users.vue&#39;)</span><br><span class="line">&#x2F;&#x2F; import Users from &#39;.&#x2F;components&#x2F;user&#x2F;Users.vue&#39;</span><br><span class="line">const Rights &#x3D; () &#x3D;&gt; import(&#x2F;* webpackChunkName:&quot;power&quot; *&#x2F; &#39;.&#x2F;components&#x2F;power&#x2F;Rights.vue&#39;)</span><br><span class="line">&#x2F;&#x2F; import Rights from &#39;.&#x2F;components&#x2F;power&#x2F;Rights.vue&#39;</span><br><span class="line">const Roles &#x3D; () &#x3D;&gt; import(&#x2F;* webpackChunkName:&quot;power&quot; *&#x2F; &#39;.&#x2F;components&#x2F;power&#x2F;Roles.vue&#39;)</span><br><span class="line">&#x2F;&#x2F; import Roles from &#39;.&#x2F;components&#x2F;power&#x2F;Roles.vue&#39;</span><br><span class="line">const Cate &#x3D; () &#x3D;&gt; import(&#x2F;* webpackChunkName:&quot;goods&quot; *&#x2F; &#39;.&#x2F;components&#x2F;goods&#x2F;Cate.vue&#39;)</span><br><span class="line">&#x2F;&#x2F; import Cate from &#39;.&#x2F;components&#x2F;goods&#x2F;Cate.vue&#39;</span><br><span class="line">const Params &#x3D; () &#x3D;&gt; import(&#x2F;* webpackChunkName:&quot;goods&quot; *&#x2F; &#39;.&#x2F;components&#x2F;goods&#x2F;Params.vue&#39;)</span><br><span class="line">&#x2F;&#x2F; import Params from &#39;.&#x2F;components&#x2F;goods&#x2F;Params.vue&#39;</span><br><span class="line">const GoodList &#x3D; () &#x3D;&gt; import(&#x2F;* webpackChunkName:&quot;goods&quot; *&#x2F; &#39;.&#x2F;components&#x2F;goods&#x2F;List.vue&#39;)</span><br><span class="line">&#x2F;&#x2F; import GoodList from &#39;.&#x2F;components&#x2F;goods&#x2F;List.vue&#39;</span><br><span class="line">const GoodAdd &#x3D; () &#x3D;&gt; import(&#x2F;* webpackChunkName:&quot;goods&quot; *&#x2F; &#39;.&#x2F;components&#x2F;goods&#x2F;Add.vue&#39;)</span><br><span class="line">&#x2F;&#x2F; import GoodAdd from &#39;.&#x2F;components&#x2F;goods&#x2F;Add.vue&#39;</span><br><span class="line">const Order &#x3D; () &#x3D;&gt; import(&#x2F;* webpackChunkName:&quot;order&quot; *&#x2F; &#39;.&#x2F;components&#x2F;order&#x2F;Order.vue&#39;)</span><br><span class="line">&#x2F;&#x2F; import Order from &#39;.&#x2F;components&#x2F;order&#x2F;Order.vue&#39;</span><br><span class="line">const Report &#x3D; () &#x3D;&gt; import(&#x2F;* webpackChunkName:&quot;report&quot; *&#x2F; &#39;.&#x2F;components&#x2F;report&#x2F;Report.vue&#39;)</span><br><span class="line">&#x2F;&#x2F; import Report from &#39;.&#x2F;components&#x2F;report&#x2F;Report.vue&#39;</span><br></pre></td></tr></table></figure>

<h1 id="16-项目上线"><a href="#16-项目上线" class="headerlink" title="16.项目上线"></a>16.项目上线</h1><h2 id="16-1-通过node创建服务器"><a href="#16-1-通过node创建服务器" class="headerlink" title="16.1 通过node创建服务器"></a>16.1 通过node创建服务器</h2><p>在vue_shop同级创建一个文件夹vue_shop_server存放node服务器<br>使用终端打开vue_shop_server文件夹，输入命令 npm init -y<br>初始化包之后，输入命令 npm i express -S<br>打开vue_shop目录，复制dist文件夹，粘贴到vue_shop_server中<br>在vue_shop_server文件夹中创建app.js文件,编写代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">const express &#x3D; require(&#39;express&#39;)</span><br><span class="line"></span><br><span class="line">const app &#x3D; express()</span><br><span class="line"></span><br><span class="line">app.use(express.static(&#39;.&#x2F;dist&#39;))</span><br><span class="line"></span><br><span class="line">app.listen(8998,()&#x3D;&gt;&#123;</span><br><span class="line">    console.log(&quot;server running at http:&#x2F;&#x2F;127.0.0.1:8998&quot;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>然后再次在终端中输入  node app.js</p>
<h2 id="16-2-开启gzip压缩"><a href="#16-2-开启gzip压缩" class="headerlink" title="16.2 开启gzip压缩"></a>16.2 开启gzip压缩</h2><p>打开vue_shop_server文件夹的终端，输入命令：npm i compression -D<br>打开app.js,编写代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">const express &#x3D; require(&#39;express&#39;)</span><br><span class="line"></span><br><span class="line">const compression &#x3D; require(&#39;compression&#39;)</span><br><span class="line"></span><br><span class="line">const app &#x3D; express()</span><br><span class="line"></span><br><span class="line">app.use(compression())</span><br><span class="line">app.use(express.static(&#39;.&#x2F;dist&#39;))</span><br><span class="line"></span><br><span class="line">app.listen(8998,()&#x3D;&gt;&#123;</span><br><span class="line">    console.log(&quot;server running at http:&#x2F;&#x2F;127.0.0.1:8998&quot;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="16-3-配置https服务"><a href="#16-3-配置https服务" class="headerlink" title="16.3 配置https服务"></a>16.3 配置https服务</h2><p>配置https服务一般是后台进行处理，前端开发人员了解即可。<br>首先，需要申请SSL证书，进入<a target="_blank" rel="noopener" href="https://freessl.cn官网/">https://freessl.cn官网</a><br>在后台导入证书，打开今天资料/素材，复制素材中的两个文件到vue_shop_server中<br>打开app.js文件，编写代码导入证书，并开启https服务</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">const express &#x3D; require(&#39;express&#39;)</span><br><span class="line">const compression &#x3D; require(&#39;compression&#39;)</span><br><span class="line">const https &#x3D; require(&#39;https&#39;)</span><br><span class="line">const fs &#x3D; require(&#39;fs&#39;)</span><br><span class="line"></span><br><span class="line">const app &#x3D; express()</span><br><span class="line">&#x2F;&#x2F;创建配置对象设置公钥和私钥</span><br><span class="line">const options &#x3D; &#123;</span><br><span class="line">    cert:fs.readFileSync(&#39;.&#x2F;full_chain.pem&#39;),</span><br><span class="line">    key:fs.readFileSync(&#39;.&#x2F;private.key&#39;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">app.use(compression())</span><br><span class="line">app.use(express.static(&#39;.&#x2F;dist&#39;))</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; app.listen(8998,()&#x3D;&gt;&#123;</span><br><span class="line">&#x2F;&#x2F;     console.log(&quot;server running at http:&#x2F;&#x2F;127.0.0.1:8998&quot;)</span><br><span class="line">&#x2F;&#x2F; &#125;)</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 启动https服务</span><br><span class="line">https.createServer(options,app).listen(443)</span><br></pre></td></tr></table></figure>

<p>注意：因为我们使用的证书有问题，所以无法正常使用https服务</p>
<h2 id="16-4-使用pm2管理应用"><a href="#16-4-使用pm2管理应用" class="headerlink" title="16.4 使用pm2管理应用"></a>16.4 使用pm2管理应用</h2><p>打开vue_shop_server文件夹的终端，输入命令：npm i pm2 -g<br>使用pm2启动项目，在终端中输入命令：pm2 start app.js –name 自定义名称<br>查看项目列表命令：pm2 ls<br>重启项目：pm2 restart 自定义名称<br>停止项目：pm2 stop 自定义名称<br>删除项目：pm2 delete 自定义名称</p>

    </div>

    
    
    
      
<div>
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
</div>
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Vue/" rel="tag"><i class="fa fa-tag"></i> Vue</a>
              <a href="/tags/%E5%AE%9E%E6%88%98/" rel="tag"><i class="fa fa-tag"></i> 实战</a>
              <a href="/tags/%E7%94%B5%E5%95%86%E7%AE%A1%E7%90%86%E5%90%8E%E5%8F%B0/" rel="tag"><i class="fa fa-tag"></i> 电商管理后台</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/10/20/Webpack%E9%9D%A2%E8%AF%95%E9%A2%98/" rel="prev" title="Weppack面试题整理">
      <i class="fa fa-chevron-left"></i> Weppack面试题整理
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/10/25/hexo%E5%8D%9A%E5%AE%A2/" rel="next" title="hexo搭建博客">
      hexo搭建博客 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-%E7%94%B5%E5%95%86%E4%B8%9A%E5%8A%A1%E6%A6%82%E8%BF%B0"><span class="nav-text">1.电商业务概述</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-%E9%A1%B9%E7%9B%AE%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">2.项目初始化</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1-%E5%AE%89%E8%A3%85Vue%E8%84%9A%E6%89%8B%E6%9E%B6"><span class="nav-text">2.1 安装Vue脚手架</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2-%E9%80%9A%E8%BF%87%E8%84%9A%E6%89%8B%E6%9E%B6%E5%88%9B%E5%BB%BA%E9%A1%B9%E7%9B%AE"><span class="nav-text">2.2 通过脚手架创建项目</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-3-%E9%85%8D%E7%BD%AE%E8%B7%AF%E7%94%B1"><span class="nav-text">2.3 配置路由</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-4-%E9%85%8D%E7%BD%AEElement-UI-%E5%9C%A8%E6%8F%92%E4%BB%B6%E4%B8%AD%E5%AE%89%E8%A3%85%EF%BC%8C%E6%90%9C%E7%B4%A2vue-cli-plugin-element"><span class="nav-text">2.4 配置Element-UI:在插件中安装，搜索vue-cli-plugin-element</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-5-%E9%85%8D%E7%BD%AEAxios%EF%BC%9A%E5%9C%A8%E4%BE%9D%E8%B5%96%E4%B8%AD%E5%AE%89%E8%A3%85-%E6%90%9C%E7%B4%A2axios-%E8%BF%90%E8%A1%8C%E4%BE%9D%E8%B5%96"><span class="nav-text">2.5 配置Axios：在依赖中安装,搜索axios(运行依赖)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-6-%E5%88%9D%E5%A7%8B%E5%8C%96git%E4%BB%93%E5%BA%93"><span class="nav-text">2.6 初始化git仓库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-7-%E5%B0%86%E6%9C%AC%E5%9C%B0%E9%A1%B9%E7%9B%AE%E6%89%98%E7%AE%A1%E5%88%B0github%E6%88%96%E8%80%85%E7%A0%81%E4%BA%91%E4%B8%AD"><span class="nav-text">2.7 将本地项目托管到github或者码云中</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-%E7%A0%81%E4%BA%91%E7%9B%B8%E5%85%B3%E6%93%8D%E4%BD%9C"><span class="nav-text">3.码云相关操作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#3-1-%E6%B3%A8%E5%86%8C%E7%99%BB%E5%BD%95%E7%A0%81%E4%BA%91%E8%B4%A6%E5%8F%B7"><span class="nav-text">3.1 注册登录码云账号</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-2-%E5%AE%89%E8%A3%85git"><span class="nav-text">3.2 安装git</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-3-%E7%82%B9%E5%87%BB%E7%BD%91%E7%AB%99%E5%8F%B3%E4%B8%8A%E8%A7%92%E2%80%9C%E7%99%BB%E5%BD%95%E2%80%9D%EF%BC%8C%E7%99%BB%E5%BD%95%E7%A0%81%E4%BA%91%EF%BC%8C%E5%B9%B6%E8%BF%9B%E8%A1%8C%E8%B4%A6%E5%8F%B7%E8%AE%BE%E7%BD%AE"><span class="nav-text">3.3 点击网站右上角“登录”，登录码云，并进行账号设置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-4-%E5%9C%A8%E6%9C%AC%E5%9C%B0%E5%88%9B%E5%BB%BA%E5%85%AC%E9%92%A5%EF%BC%9A"><span class="nav-text">3.4 在本地创建公钥：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-5-%E6%89%BE%E5%88%B0%E5%85%AC%E9%92%A5%E5%9C%B0%E5%9D%80%EF%BC%9A"><span class="nav-text">3.5 找到公钥地址：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-6-%E6%B5%8B%E8%AF%95%E5%85%AC%E9%92%A5%EF%BC%9A%E6%89%93%E5%BC%80%E7%BB%88%E7%AB%AF%EF%BC%8C%E8%BE%93%E5%85%A5%E5%91%BD%E4%BB%A4"><span class="nav-text">3.6 测试公钥：打开终端，输入命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-7-%E5%B0%86%E6%9C%AC%E5%9C%B0%E4%BB%A3%E7%A0%81%E6%89%98%E7%AE%A1%E5%88%B0%E7%A0%81%E4%BA%91%E4%B8%AD"><span class="nav-text">3.7 将本地代码托管到码云中</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-8-%E8%BF%9B%E8%A1%8Cgit%E9%85%8D%E7%BD%AE%EF%BC%9A"><span class="nav-text">3.8 进行git配置：</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4-%E9%85%8D%E7%BD%AE%E5%90%8E%E5%8F%B0%E9%A1%B9%E7%9B%AE"><span class="nav-text">4.配置后台项目</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#4-1-%E5%AE%89%E8%A3%85phpStudy%E5%B9%B6%E5%AF%BC%E5%85%A5mysql%E6%95%B0%E6%8D%AE%E5%BA%93%E6%95%B0%E6%8D%AE"><span class="nav-text">4.1 安装phpStudy并导入mysql数据库数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-2-%E5%AE%89%E8%A3%85nodeJS%EF%BC%8C%E9%85%8D%E7%BD%AE%E5%90%8E%E5%8F%B0%E9%A1%B9%E7%9B%AE-%E4%BB%8E%E7%BB%88%E7%AB%AF%E6%89%93%E5%BC%80%E5%90%8E%E5%8F%B0%E9%A1%B9%E7%9B%AEvue-api-server"><span class="nav-text">4.2 安装nodeJS，配置后台项目,从终端打开后台项目vue_api_server</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-3-%E4%BD%BF%E7%94%A8postman%E6%B5%8B%E8%AF%95api%E6%8E%A5%E5%8F%A3"><span class="nav-text">4.3 使用postman测试api接口</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#5-%E5%AE%9E%E7%8E%B0%E7%99%BB%E5%BD%95%E5%8A%9F%E8%83%BD"><span class="nav-text">5.实现登录功能</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#5-1-%E7%99%BB%E5%BD%95%E7%8A%B6%E6%80%81%E4%BF%9D%E6%8C%81"><span class="nav-text">5.1 登录状态保持</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-2-%E7%99%BB%E5%BD%95%E9%80%BB%E8%BE%91%EF%BC%9A"><span class="nav-text">5.2 登录逻辑：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-3-%E6%B7%BB%E5%8A%A0%E6%96%B0%E5%88%86%E6%94%AFlogin%EF%BC%8C%E5%9C%A8login%E5%88%86%E6%94%AF%E4%B8%AD%E5%BC%80%E5%8F%91%E5%BD%93%E5%89%8D%E9%A1%B9%E7%9B%AEvue-shop%EF%BC%9A"><span class="nav-text">5.3 添加新分支login，在login分支中开发当前项目vue_shop：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-3-1-%E6%B7%BB%E5%8A%A0element-ui%E7%9A%84%E8%A1%A8%E5%8D%95%E7%BB%84%E4%BB%B6"><span class="nav-text">5.3.1 添加element-ui的表单组件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-3-2-%E6%B7%BB%E5%8A%A0%E7%AC%AC%E4%B8%89%E6%96%B9%E5%AD%97%E4%BD%93"><span class="nav-text">5.3.2 添加第三方字体</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-3-3-%E6%B7%BB%E5%8A%A0%E8%A1%A8%E5%8D%95%E9%AA%8C%E8%AF%81%E7%9A%84%E6%AD%A5%E9%AA%A4"><span class="nav-text">5.3.3 添加表单验证的步骤</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-%E7%BB%99%E6%B7%BB%E5%8A%A0%E5%B1%9E%E6%80%A7-rules-%E2%80%9Drules%E2%80%9D%EF%BC%8Crules%E6%98%AF%E4%B8%80%E5%A0%86%E9%AA%8C%E8%AF%81%E8%A7%84%E5%88%99%EF%BC%8C%E5%AE%9A%E4%B9%89%E5%9C%A8script%E3%80%81%E4%B8%AD"><span class="nav-text">1).给添加属性:rules&#x3D;”rules”，rules是一堆验证规则，定义在script、中</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-%E5%9C%A8script%E4%B8%AD%E6%B7%BB%E5%8A%A0rules%EF%BC%9Aexport-default-data-return-%E2%80%A6%E2%80%A6-rules"><span class="nav-text">2).在script中添加rules：export default{ data(){return{……, rules: {</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-%E9%80%9A%E8%BF%87%E7%9A%84prop%E5%B1%9E%E6%80%A7%E8%AE%BE%E7%BD%AE%E9%AA%8C%E8%AF%81%E8%A7%84%E5%88%99"><span class="nav-text">3).通过的prop属性设置验证规则</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-%E5%AF%BC%E5%85%A5axios%E4%BB%A5%E5%8F%91%E9%80%81ajax%E8%AF%B7%E6%B1%82"><span class="nav-text">4.导入axios以发送ajax请求</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-%E9%85%8D%E7%BD%AE%E5%BC%B9%E7%AA%97%E6%8F%90%E7%A4%BA%EF%BC%9A"><span class="nav-text">5.配置弹窗提示：</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-4-%E7%99%BB%E5%BD%95%E6%88%90%E5%8A%9F%E4%B9%8B%E5%90%8E%E7%9A%84%E6%93%8D%E4%BD%9C"><span class="nav-text">5.4 登录成功之后的操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-4-1-%E7%99%BB%E5%BD%95%E6%88%90%E5%8A%9F%E4%B9%8B%E5%90%8E%EF%BC%8C%E9%9C%80%E8%A6%81%E5%B0%86%E5%90%8E%E5%8F%B0%E8%BF%94%E5%9B%9E%E7%9A%84token%E4%BF%9D%E5%AD%98%E5%88%B0sessionStorage%E4%B8%AD"><span class="nav-text">5.4.1 登录成功之后，需要将后台返回的token保存到sessionStorage中</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-5-%E5%A4%84%E7%90%86ESLint%E8%AD%A6%E5%91%8A"><span class="nav-text">5.5 处理ESLint警告</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-6-%E5%90%88%E5%B9%B6%E6%8C%89%E9%9C%80%E5%AF%BC%E5%85%A5%E7%9A%84element-ui"><span class="nav-text">5.6 合并按需导入的element-ui</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-7-%E5%B0%86%E4%BB%A3%E7%A0%81%E6%8F%90%E4%BA%A4%E5%88%B0%E7%A0%81%E4%BA%91"><span class="nav-text">5.7 将代码提交到码云</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#6-%E5%90%8E%E5%8F%B0%E9%A6%96%E9%A1%B5%E5%B8%83%E5%B1%80"><span class="nav-text">6.后台首页布局</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#6-1-git-%E6%93%8D%E4%BD%9C"><span class="nav-text">6.1 git 操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-2-%E9%A6%96%E9%A1%B5%E6%95%B4%E4%BD%93%E5%B8%83%E5%B1%80"><span class="nav-text">6.2 首页整体布局</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-3-%E9%A1%B6%E9%83%A8%E5%B8%83%E5%B1%80%EF%BC%8C%E4%BE%A7%E8%BE%B9%E6%A0%8F%E5%B8%83%E5%B1%80"><span class="nav-text">6.3 顶部布局，侧边栏布局</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-4-axios%E8%AF%B7%E6%B1%82%E6%8B%A6%E6%88%AA%E5%99%A8"><span class="nav-text">6.4 axios请求拦截器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-5-%E8%AF%B7%E6%B1%82%E4%BE%A7%E8%BE%B9%E6%A0%8F%E6%95%B0%E6%8D%AE"><span class="nav-text">6.5 请求侧边栏数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-6-%E8%AE%BE%E7%BD%AE%E6%BF%80%E6%B4%BB%E5%AD%90%E8%8F%9C%E5%8D%95%E6%A0%B7%E5%BC%8F"><span class="nav-text">6.6 设置激活子菜单样式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-7-%E5%88%B6%E4%BD%9C%E4%BE%A7%E8%BE%B9%E8%8F%9C%E5%8D%95%E6%A0%8F%E7%9A%84%E4%BC%B8%E7%BC%A9%E5%8A%9F%E8%83%BD"><span class="nav-text">6.7 制作侧边菜单栏的伸缩功能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-8-%E5%9C%A8%E5%90%8E%E5%8F%B0%E9%A6%96%E9%A1%B5%E6%B7%BB%E5%8A%A0%E5%AD%90%E7%BA%A7%E8%B7%AF%E7%94%B1"><span class="nav-text">6.8 在后台首页添加子级路由</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#7-%E5%AE%8C%E6%88%90%E7%94%A8%E6%88%B7%E5%88%97%E8%A1%A8%E5%BC%80%E5%8F%91"><span class="nav-text">7.完成用户列表开发</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#7-1-%E5%AE%8C%E6%88%90%E7%94%A8%E6%88%B7%E5%88%97%E8%A1%A8%E4%B8%BB%E4%BD%93%E5%8C%BA%E5%9F%9F"><span class="nav-text">7.1 完成用户列表主体区域</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-2-%E7%BB%98%E5%88%B6%E7%94%A8%E6%88%B7%E5%88%97%E8%A1%A8%E5%9F%BA%E6%9C%AC%E7%BB%93%E6%9E%84"><span class="nav-text">7.2 绘制用户列表基本结构</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#7-2-1-%E4%BD%BF%E7%94%A8element-ui%E9%9D%A2%E5%8C%85%E5%B1%91%E7%BB%84%E4%BB%B6%E5%AE%8C%E6%88%90%E9%A1%B6%E9%83%A8%E5%AF%BC%E8%88%AA%E8%B7%AF%E5%BE%84"><span class="nav-text">7.2.1 使用element-ui面包屑组件完成顶部导航路径</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-2-2-%E4%BD%BF%E7%94%A8element-ui%E5%8D%A1%E7%89%87%E7%BB%84%E4%BB%B6%E5%AE%8C%E6%88%90%E4%B8%BB%E4%BD%93%E8%A1%A8%E6%A0%BC"><span class="nav-text">7.2.2 使用element-ui卡片组件完成主体表格</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-3-%E8%AF%B7%E6%B1%82%E7%94%A8%E6%88%B7%E5%88%97%E8%A1%A8%E6%95%B0%E6%8D%AE"><span class="nav-text">7.3 请求用户列表数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-4-%E5%B0%86%E7%94%A8%E6%88%B7%E5%88%97%E8%A1%A8%E6%95%B0%E6%8D%AE%E5%B1%95%E7%A4%BA"><span class="nav-text">7.4 将用户列表数据展示</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-5-%E5%AE%9E%E7%8E%B0%E7%94%A8%E6%88%B7%E5%88%97%E8%A1%A8%E5%88%86%E9%A1%B5"><span class="nav-text">7.5 实现用户列表分页</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#7-5-1%E4%BD%BF%E7%94%A8%E8%A1%A8%E6%A0%BC%E6%9D%A5%E5%B1%95%E7%A4%BA%E7%94%A8%E6%88%B7%E5%88%97%E8%A1%A8%E6%95%B0%E6%8D%AE"><span class="nav-text">7.5.1使用表格来展示用户列表数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-5-2-%E6%9B%B4%E6%94%B9%E7%BB%84%E4%BB%B6%E4%B8%AD%E7%9A%84%E7%BB%91%E5%AE%9A%E6%95%B0%E6%8D%AE"><span class="nav-text">7.5.2 更改组件中的绑定数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-5-3-%E6%B7%BB%E5%8A%A0%E4%B8%A4%E4%B8%AA%E4%BA%8B%E4%BB%B6%E7%9A%84%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86%E5%87%BD%E6%95%B0-size-change%EF%BC%8C-current-change"><span class="nav-text">7.5.3 添加两个事件的事件处理函数@size-change，@current-change</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-6-%E5%AE%9E%E7%8E%B0%E6%9B%B4%E6%96%B0%E7%94%A8%E6%88%B7%E7%8A%B6%E6%80%81"><span class="nav-text">7.6 实现更新用户状态</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#7-6-1-%E9%A6%96%E5%85%88%E7%9B%91%E5%90%AC%E7%94%A8%E6%88%B7%E7%82%B9%E5%87%BBswitch%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BA%8B%E4%BB%B6%EF%BC%8C%E5%B9%B6%E5%B0%86%E4%BD%9C%E7%94%A8%E5%9F%9F%E6%8F%92%E6%A7%BD%E7%9A%84%E6%95%B0%E6%8D%AE%E5%BD%93%E5%81%9A%E4%BA%8B%E4%BB%B6%E5%8F%82%E6%95%B0%E8%BF%9B%E8%A1%8C%E4%BC%A0%E9%80%92"><span class="nav-text">7.6.1 首先监听用户点击switch组件的事件，并将作用域插槽的数据当做事件参数进行传递</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-6-2-%E5%9C%A8%E4%BA%8B%E4%BB%B6%E4%B8%AD%E5%8F%91%E9%80%81%E8%AF%B7%E6%B1%82%E5%AE%8C%E6%88%90%E7%8A%B6%E6%80%81%E7%9A%84%E6%9B%B4%E6%94%B9"><span class="nav-text">7.6.2 在事件中发送请求完成状态的更改</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-7-%E5%AE%9E%E7%8E%B0%E6%90%9C%E7%B4%A2%E5%8A%9F%E8%83%BD"><span class="nav-text">7.7 实现搜索功能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-8-%E5%AE%9E%E7%8E%B0%E6%B7%BB%E5%8A%A0%E7%94%A8%E6%88%B7"><span class="nav-text">7.8 实现添加用户</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#7-8-1-%E5%BD%93%E6%88%91%E4%BB%AC%E7%82%B9%E5%87%BB%E6%B7%BB%E5%8A%A0%E7%94%A8%E6%88%B7%E6%8C%89%E9%92%AE%E7%9A%84%E6%97%B6%E5%80%99%EF%BC%8C%E5%BC%B9%E5%87%BA%E4%B8%80%E4%B8%AA%E5%AF%B9%E8%AF%9D%E6%A1%86%E6%9D%A5%E5%AE%9E%E7%8E%B0%E6%B7%BB%E5%8A%A0%E7%94%A8%E6%88%B7%E7%9A%84%E5%8A%9F%E8%83%BD%EF%BC%8C%E9%A6%96%E5%85%88%E6%88%91%E4%BB%AC%E9%9C%80%E8%A6%81%E5%A4%8D%E5%88%B6%E5%AF%B9%E8%AF%9D%E6%A1%86%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BB%A3%E7%A0%81%E5%B9%B6%E5%9C%A8element-js%E6%96%87%E4%BB%B6%E4%B8%AD%E5%BC%95%E5%85%A5Dialog%E7%BB%84%E4%BB%B6"><span class="nav-text">7.8.1 当我们点击添加用户按钮的时候，弹出一个对话框来实现添加用户的功能，首先我们需要复制对话框组件的代码并在element.js文件中引入Dialog组件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-8-2-%E6%8E%A5%E4%B8%8B%E6%9D%A5%E6%88%91%E4%BB%AC%E8%A6%81%E4%B8%BA%E2%80%9C%E6%B7%BB%E5%8A%A0%E7%94%A8%E6%88%B7%E2%80%9D%E6%8C%89%E9%92%AE%E6%B7%BB%E5%8A%A0%E7%82%B9%E5%87%BB%E4%BA%8B%E4%BB%B6%EF%BC%8C%E5%9C%A8%E4%BA%8B%E4%BB%B6%E4%B8%AD%E5%B0%86addDialogVisible%E8%AE%BE%E7%BD%AE%E4%B8%BAtrue%EF%BC%8C%E5%8D%B3%E6%98%BE%E7%A4%BA%E5%AF%B9%E8%AF%9D%E6%A1%86"><span class="nav-text">7.8.2 接下来我们要为“添加用户”按钮添加点击事件，在事件中将addDialogVisible设置为true，即显示对话框</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-8-3-%E6%9B%B4%E6%94%B9Dialog%E7%BB%84%E4%BB%B6%E4%B8%AD%E7%9A%84%E5%86%85%E5%AE%B9"><span class="nav-text">7.8.3 更改Dialog组件中的内容</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-8-4-%E6%B7%BB%E5%8A%A0%E6%95%B0%E6%8D%AE%E7%BB%91%E5%AE%9A%E5%92%8C%E6%A0%A1%E9%AA%8C%E8%A7%84%E5%88%99%EF%BC%9A"><span class="nav-text">7.8.4 添加数据绑定和校验规则：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-8-5%E5%BD%93%E5%85%B3%E9%97%AD%E5%AF%B9%E8%AF%9D%E6%A1%86%E6%97%B6%EF%BC%8C%E9%87%8D%E7%BD%AE%E8%A1%A8%E5%8D%95"><span class="nav-text">7.8.5当关闭对话框时，重置表单</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-8-6-%E7%82%B9%E5%87%BB%E5%AF%B9%E8%AF%9D%E6%A1%86%E4%B8%AD%E7%9A%84%E7%A1%AE%E5%AE%9A%E6%8C%89%E9%92%AE%EF%BC%8C%E5%8F%91%E9%80%81%E8%AF%B7%E6%B1%82%E5%AE%8C%E6%88%90%E6%B7%BB%E5%8A%A0%E7%94%A8%E6%88%B7%E7%9A%84%E6%93%8D%E4%BD%9C"><span class="nav-text">7.8.6 点击对话框中的确定按钮，发送请求完成添加用户的操作</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-9-%E6%8E%A8%E9%80%81%E4%BB%A3%E7%A0%81"><span class="nav-text">7.9 推送代码</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#8-%E5%AE%8C%E6%88%90%E7%94%A8%E6%88%B7%E4%BF%A1%E6%81%AF%E7%9A%84%E4%BF%AE%E6%94%B9%E4%B8%8E%E5%88%A0%E9%99%A4"><span class="nav-text">8.完成用户信息的修改与删除</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#8-1-git%E6%93%8D%E4%BD%9C"><span class="nav-text">8.1 git操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-2-%E4%BF%AE%E6%94%B9%E7%94%A8%E6%88%B7%E4%BF%A1%E6%81%AF"><span class="nav-text">8.2 修改用户信息</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#8-2-1-%E4%B8%BA%E7%94%A8%E6%88%B7%E5%88%97%E8%A1%A8%E4%B8%AD%E7%9A%84%E4%BF%AE%E6%94%B9%E6%8C%89%E9%92%AE%E7%BB%91%E5%AE%9A%E7%82%B9%E5%87%BB%E4%BA%8B%E4%BB%B6"><span class="nav-text">8.2.1 为用户列表中的修改按钮绑定点击事件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-2-2-%E5%9C%A8%E9%A1%B5%E9%9D%A2%E4%B8%AD%E6%B7%BB%E5%8A%A0%E4%BF%AE%E6%94%B9%E7%94%A8%E6%88%B7%E5%AF%B9%E8%AF%9D%E6%A1%86%EF%BC%8C%E5%B9%B6%E4%BF%AE%E6%94%B9%E5%AF%B9%E8%AF%9D%E6%A1%86%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="nav-text">8.2.2 在页面中添加修改用户对话框，并修改对话框的属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-2-3-%E6%A0%B9%E6%8D%AEid%E6%9F%A5%E8%AF%A2%E9%9C%80%E8%A6%81%E4%BF%AE%E6%94%B9%E7%9A%84%E7%94%A8%E6%88%B7%E6%95%B0%E6%8D%AE"><span class="nav-text">8.2.3 根据id查询需要修改的用户数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-2-4-%E5%9C%A8%E5%BC%B9%E5%87%BA%E7%AA%97%E4%B8%AD%E6%B7%BB%E5%8A%A0%E4%BF%AE%E6%94%B9%E7%94%A8%E6%88%B7%E4%BF%A1%E6%81%AF%E7%9A%84%E8%A1%A8%E5%8D%95%E5%B9%B6%E5%81%9A%E5%93%8D%E5%BA%94%E7%9A%84%E6%95%B0%E6%8D%AE%E7%BB%91%E5%AE%9A%E4%BB%A5%E5%8F%8A%E6%95%B0%E6%8D%AE%E9%AA%8C%E8%AF%81"><span class="nav-text">8.2.4 在弹出窗中添加修改用户信息的表单并做响应的数据绑定以及数据验证</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-2-5-%E7%9B%91%E5%90%AC%E5%AF%B9%E8%AF%9D%E6%A1%86%E5%85%B3%E9%97%AD%E4%BA%8B%E4%BB%B6%EF%BC%8C%E5%9C%A8%E5%AF%B9%E8%AF%9D%E6%A1%86%E5%85%B3%E9%97%AD%E4%B9%8B%E5%90%8E%EF%BC%8C%E9%87%8D%E7%BD%AE%E8%A1%A8%E5%8D%95"><span class="nav-text">8.2.5 监听对话框关闭事件，在对话框关闭之后，重置表单</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-2-6-%E5%9C%A8%E7%94%A8%E6%88%B7%E7%82%B9%E5%87%BB%E7%A1%AE%E5%AE%9A%E6%8C%89%E9%92%AE%E7%9A%84%E6%97%B6%E5%80%99%EF%BC%8C%E9%AA%8C%E8%AF%81%E6%95%B0%E6%8D%AE%E6%88%90%E5%8A%9F%E4%B9%8B%E5%90%8E%E5%8F%91%E9%80%81%E8%AF%B7%E6%B1%82%E5%AE%8C%E6%88%90%E4%BF%AE%E6%94%B9"><span class="nav-text">8.2.6 在用户点击确定按钮的时候，验证数据成功之后发送请求完成修改</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-3-%E5%88%A0%E9%99%A4%E7%94%A8%E6%88%B7"><span class="nav-text">8.3 删除用户</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#8-3-1-%E5%AF%BC%E5%85%A5MessageBox%E7%BB%84%E4%BB%B6%EF%BC%8C%E5%B9%B6%E5%B0%86MessageBox%E7%BB%84%E4%BB%B6%E6%8C%82%E8%BD%BD%E5%88%B0%E5%AE%9E%E4%BE%8B%E3%80%82"><span class="nav-text">8.3.1 导入MessageBox组件，并将MessageBox组件挂载到实例。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-3-2-%E7%BB%99%E7%94%A8%E6%88%B7%E5%88%97%E8%A1%A8%E4%B8%AD%E7%9A%84%E5%88%A0%E9%99%A4%E6%8C%89%E9%92%AE%E6%B7%BB%E5%8A%A0%E4%BA%8B%E4%BB%B6%EF%BC%8C%E5%B9%B6%E5%9C%A8%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86%E5%87%BD%E6%95%B0%E4%B8%AD%E5%BC%B9%E5%87%BA%E7%A1%AE%E5%AE%9A%E5%8F%96%E6%B6%88%E7%AA%97-%E6%9C%80%E5%90%8E%E5%86%8D%E6%A0%B9%E6%8D%AEid%E5%8F%91%E9%80%81%E5%88%A0%E9%99%A4%E7%94%A8%E6%88%B7%E7%9A%84%E8%AF%B7%E6%B1%82"><span class="nav-text">8.3.2 给用户列表中的删除按钮添加事件，并在事件处理函数中弹出确定取消窗,最后再根据id发送删除用户的请求</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-4-%E6%8E%A8%E9%80%81%E4%BB%A3%E7%A0%81"><span class="nav-text">8.4 推送代码</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#9-%E6%9D%83%E9%99%90%E5%8A%9F%E8%83%BD%E5%BC%80%E5%8F%91"><span class="nav-text">9.权限功能开发</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#9-1-git-%E6%93%8D%E4%BD%9C"><span class="nav-text">9.1 git 操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-2-%E6%9D%83%E9%99%90%E5%88%97%E8%A1%A8"><span class="nav-text">9.2 权限列表</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#9-2-1-%E6%B7%BB%E5%8A%A0%E6%9D%83%E9%99%90%E5%88%97%E8%A1%A8%E8%B7%AF%E7%94%B1"><span class="nav-text">9.2.1 添加权限列表路由</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-2-2-%E6%B7%BB%E5%8A%A0%E9%9D%A2%E5%8C%85%E5%B1%91%E5%AF%BC%E8%88%AA"><span class="nav-text">9.2.2 添加面包屑导航</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-2-3-%E6%98%BE%E7%A4%BA%E6%95%B0%E6%8D%AE"><span class="nav-text">9.2.3 显示数据</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-3-%E8%A7%92%E8%89%B2%E5%88%97%E8%A1%A8"><span class="nav-text">9.3 角色列表</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#9-3-1-%E6%B7%BB%E5%8A%A0%E8%A7%92%E8%89%B2%E5%88%97%E8%A1%A8%E8%B7%AF%E7%94%B1"><span class="nav-text">9.3.1 添加角色列表路由</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-3-2-%E6%B7%BB%E5%8A%A0%E9%9D%A2%E5%8C%85%E5%B1%91%E5%AF%BC%E8%88%AA"><span class="nav-text">9.3.2 添加面包屑导航</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-3-3-%E6%98%BE%E7%A4%BA%E6%95%B0%E6%8D%AE"><span class="nav-text">9.3.3 显示数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-3-4-%E6%B7%BB%E5%8A%A0%E8%A7%92%E8%89%B2%EF%BC%8C%E5%88%A0%E9%99%A4%E8%A7%92%E8%89%B2%EF%BC%8C%E7%BC%96%E8%BE%91%E8%A7%92%E8%89%B2"><span class="nav-text">9.3.4 添加角色，删除角色，编辑角色</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-3-5-%E7%94%9F%E6%88%90%E6%9D%83%E9%99%90%E5%88%97%E8%A1%A8"><span class="nav-text">9.3.5 生成权限列表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-3-6-%E7%BE%8E%E5%8C%96%E6%A0%B7%E5%BC%8F"><span class="nav-text">9.3.6 美化样式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-3-7-%E6%B7%BB%E5%8A%A0%E6%9D%83%E9%99%90%E5%88%A0%E9%99%A4%E5%8A%9F%E8%83%BD"><span class="nav-text">9.3.7 添加权限删除功能</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-3-8-%E5%AE%8C%E6%88%90%E6%9D%83%E9%99%90%E5%88%86%E9%85%8D%E5%8A%9F%E8%83%BD"><span class="nav-text">9.3.8 完成权限分配功能</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-3-9-%E5%AE%8C%E6%88%90%E6%A0%91%E5%BD%A2%E7%BB%93%E6%9E%84%E5%BC%B9%E7%AA%97"><span class="nav-text">9.3.9 完成树形结构弹窗</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-4-%E5%88%86%E9%85%8D%E8%A7%92%E8%89%B2"><span class="nav-text">9.4 分配角色</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#9-4-1-%E6%B7%BB%E5%8A%A0%E5%88%86%E9%85%8D%E8%A7%92%E8%89%B2%E5%AF%B9%E8%AF%9D%E6%A1%86"><span class="nav-text">9.4.1 添加分配角色对话框</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-4-2-%E7%BB%99%E5%88%86%E9%85%8D%E8%A7%92%E8%89%B2%E6%8C%89%E9%92%AE%E6%B7%BB%E5%8A%A0%E7%82%B9%E5%87%BB%E4%BA%8B%E4%BB%B6%EF%BC%8C%E7%82%B9%E5%87%BB%E4%B9%8B%E5%90%8E%E5%BC%B9%E5%87%BA%E4%B8%80%E4%B8%AA%E5%AF%B9%E8%AF%9D%E6%A1%86%E8%BF%9B%E8%A1%8C%E8%A7%92%E8%89%B2%E5%88%86%E9%85%8D"><span class="nav-text">9.4.2 给分配角色按钮添加点击事件，点击之后弹出一个对话框进行角色分配</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-4-3-%E5%9C%A8element-js%E4%B8%AD%E5%BC%95%E5%85%A5Select%EF%BC%8COption%EF%BC%8C%E6%B3%A8%E5%86%8CSelect%EF%BC%8COption"><span class="nav-text">9.4.3 在element.js中引入Select，Option，注册Select，Option</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-4-4-%E5%BD%93%E7%94%A8%E6%88%B7%E7%82%B9%E5%87%BB%E5%AF%B9%E8%AF%9D%E6%A1%86%E4%B8%AD%E7%9A%84%E7%A1%AE%E5%AE%9A%E4%B9%8B%E5%90%8E%EF%BC%8C%E5%AE%8C%E6%88%90%E5%88%86%E9%85%8D%E8%A7%92%E8%89%B2%E7%9A%84%E6%93%8D%E4%BD%9C"><span class="nav-text">9.4.4 当用户点击对话框中的确定之后，完成分配角色的操作</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-5-%E5%B0%86%E4%BB%A3%E7%A0%81%E6%8E%A8%E9%80%81%E5%88%B0%E7%A0%81%E4%BA%91"><span class="nav-text">9.5 将代码推送到码云</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#10-%E5%95%86%E5%93%81%E5%88%86%E7%B1%BB"><span class="nav-text">10.商品分类</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#10-1-%E6%96%B0%E5%BB%BA%E5%88%86%E6%94%AFgoods-cate"><span class="nav-text">10.1 新建分支goods_cate</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-2-%E5%88%9B%E5%BB%BA%E5%AD%90%E7%BA%A7%E8%B7%AF%E7%94%B1"><span class="nav-text">10.2 创建子级路由</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-3-%E6%B7%BB%E5%8A%A0%E7%BB%84%E4%BB%B6%E5%9F%BA%E6%9C%AC%E5%B8%83%E5%B1%80"><span class="nav-text">10.3 添加组件基本布局</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-4-%E8%AF%B7%E6%B1%82%E5%88%86%E7%B1%BB%E6%95%B0%E6%8D%AE"><span class="nav-text">10.4 请求分类数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-5-%E4%BD%BF%E7%94%A8%E6%8F%92%E4%BB%B6%E5%B1%95%E7%A4%BA%E6%95%B0%E6%8D%AE"><span class="nav-text">10.5 使用插件展示数据</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#10-5-1-%E5%9C%A8vue-%E6%8E%A7%E5%88%B6%E5%8F%B0%E4%B8%AD%E7%82%B9%E5%87%BB%E4%BE%9D%E8%B5%96-gt-%E5%AE%89%E8%A3%85%E4%BE%9D%E8%B5%96-gt-%E8%BF%90%E8%A1%8C%E4%BE%9D%E8%B5%96-gt-%E8%BE%93%E5%85%A5vue-table-with-tree-gird-gt-%E7%82%B9%E5%87%BB%E5%AE%89%E8%A3%85"><span class="nav-text">10.5.1 在vue 控制台中点击依赖-&gt;安装依赖-&gt;运行依赖-&gt;输入vue-table-with-tree-gird-&gt;点击安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#10-5-2-%E6%89%93%E5%BC%80main-js%EF%BC%8C%E5%AF%BC%E5%85%A5vue-table-with-tree-grid"><span class="nav-text">10.5.2 打开main.js，导入vue-table-with-tree-grid</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#10-5-3-%E4%BD%BF%E7%94%A8%E7%BB%84%E4%BB%B6%E5%B1%95%E7%A4%BA%E5%88%86%E7%B1%BB%E6%95%B0%E6%8D%AE"><span class="nav-text">10.5.3 使用组件展示分类数据</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-6-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%95%B0%E6%8D%AE%E5%88%97"><span class="nav-text">10.6 自定义数据列</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-7-%E5%AE%8C%E6%88%90%E5%88%86%E9%A1%B5%E5%8A%9F%E8%83%BD"><span class="nav-text">10.7 完成分页功能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-8-%E5%AE%8C%E6%88%90%E6%B7%BB%E5%8A%A0%E5%88%86%E7%B1%BB"><span class="nav-text">10.8 完成添加分类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-9-%E5%AE%8C%E6%88%90%E7%BC%96%E8%BE%91%E5%92%8C%E5%88%A0%E9%99%A4%E5%8A%9F%E8%83%BD"><span class="nav-text">10.9 完成编辑和删除功能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-10-%E6%8E%A8%E9%80%81%E4%BB%A3%E7%A0%81"><span class="nav-text">10.10 推送代码</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#11-%E5%8F%82%E6%95%B0%E7%AE%A1%E7%90%86"><span class="nav-text">11.参数管理</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#11-1-%E6%96%B0%E5%BB%BA%E5%88%86%E6%94%AFgoods-params"><span class="nav-text">11.1 新建分支goods_params</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-2-%E6%B7%BB%E5%8A%A0%E5%AD%90%E7%BA%A7%E7%BB%84%E4%BB%B6"><span class="nav-text">11.2 添加子级组件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-3-%E5%AE%8C%E6%88%90%E7%BB%84%E4%BB%B6%E5%9F%BA%E6%9C%AC%E5%B8%83%E5%B1%80"><span class="nav-text">11.3 完成组件基本布局</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-4-%E5%AE%8C%E6%88%90%E7%BA%A7%E8%81%94%E9%80%89%E6%8B%A9%E6%A1%86"><span class="nav-text">11.4 完成级联选择框</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-5-%E5%B1%95%E7%A4%BA%E5%8F%82%E6%95%B0"><span class="nav-text">11.5 展示参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-6-%E6%B7%BB%E5%8A%A0%E5%8F%82%E6%95%B0"><span class="nav-text">11.6 添加参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-7-%E7%BC%96%E8%BE%91%E5%8F%82%E6%95%B0"><span class="nav-text">11.7 编辑参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-8-%E5%88%A0%E9%99%A4%E5%8F%82%E6%95%B0"><span class="nav-text">11.8 删除参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-9-%E5%B1%95%E7%A4%BA%E5%8A%A8%E6%80%81%E5%8F%82%E6%95%B0%E5%8F%AF%E9%80%89%E9%A1%B9"><span class="nav-text">11.9 展示动态参数可选项</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-10-%E6%B7%BB%E5%8A%A0-%E5%88%A0%E9%99%A4%E5%8F%AF%E9%80%89%E9%A1%B9"><span class="nav-text">11.10 添加&#x2F;删除可选项</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-11-%E6%8E%A8%E9%80%81%E4%BB%A3%E7%A0%81%E5%88%B0%E7%A0%81%E4%BA%91"><span class="nav-text">11.11 推送代码到码云</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#12-%E5%95%86%E5%93%81%E5%88%97%E8%A1%A8"><span class="nav-text">12.商品列表</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#12-1-%E5%88%9B%E5%BB%BA%E5%AD%90%E5%88%86%E6%94%AFgoods-list"><span class="nav-text">12.1 创建子分支goods_list</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#12-2-%E5%88%B6%E4%BD%9C%E5%95%86%E5%93%81%E5%88%97%E8%A1%A8%E5%9F%BA%E6%9C%AC%E7%BB%93%E6%9E%84"><span class="nav-text">12.2 制作商品列表基本结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#12-3-%E6%95%B0%E6%8D%AE%E5%B1%95%E7%A4%BA"><span class="nav-text">12.3 数据展示</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#12-4-%E5%AE%9E%E7%8E%B0%E5%88%A0%E9%99%A4%E5%95%86%E5%93%81"><span class="nav-text">12.4 实现删除商品</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#12-5-%E5%AE%9E%E7%8E%B0%E6%B7%BB%E5%8A%A0%E5%95%86%E5%93%81"><span class="nav-text">12.5 实现添加商品</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#12-5-1-%E6%B7%BB%E5%8A%A0%E7%BC%96%E7%A8%8B%E5%BC%8F%E5%AF%BC%E8%88%AA"><span class="nav-text">12.5.1 添加编程式导航</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#12-5-2-%E5%B8%83%E5%B1%80Add-vue%E7%BB%84%E4%BB%B6"><span class="nav-text">12.5.2 布局Add.vue组件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#12-5-3-%E6%B7%BB%E5%8A%A0tab%E6%A0%8F%E5%88%87%E6%8D%A2%E9%AA%8C%E8%AF%81"><span class="nav-text">12.5.3 添加tab栏切换验证</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#12-5-4-%E5%B1%95%E7%A4%BA%E4%BF%A1%E6%81%AF"><span class="nav-text">12.5.4 展示信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#12-5-5-%E5%AE%8C%E6%88%90%E5%9B%BE%E7%89%87%E4%B8%8A%E4%BC%A0"><span class="nav-text">12.5.5 完成图片上传</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#12-5-6-%E4%BD%BF%E7%94%A8%E5%AF%8C%E6%96%87%E6%9C%AC%E6%8F%92%E4%BB%B6"><span class="nav-text">12.5.6 使用富文本插件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#12-6-%E6%B7%BB%E5%8A%A0%E5%95%86%E5%93%81"><span class="nav-text">12.6 添加商品</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#12-7-%E6%8E%A8%E9%80%81%E4%BB%A3%E7%A0%81"><span class="nav-text">12.7 推送代码</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#13-%E8%AE%A2%E5%8D%95%E5%88%97%E8%A1%A8"><span class="nav-text">13.订单列表</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#13-1-%E5%88%9B%E5%BB%BA%E5%88%86%E6%94%AF"><span class="nav-text">13.1 创建分支</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#13-2-%E5%88%9B%E5%BB%BA%E8%B7%AF%E7%94%B1"><span class="nav-text">13.2 创建路由</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#13-3-%E5%AE%9E%E7%8E%B0%E6%95%B0%E6%8D%AE%E5%B1%95%E7%A4%BA%E5%8F%8A%E5%88%86%E9%A1%B5"><span class="nav-text">13.3 实现数据展示及分页</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#13-4-%E5%88%B6%E4%BD%9C%E7%9C%81%E5%B8%82%E5%8C%BA%E5%8E%BF%E8%81%94%E5%8A%A8"><span class="nav-text">13.4 制作省市区县联动</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#13-5-%E5%88%B6%E4%BD%9C%E7%89%A9%E6%B5%81%E8%BF%9B%E5%BA%A6%E5%AF%B9%E8%AF%9D%E6%A1%86"><span class="nav-text">13.5 制作物流进度对话框</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#13-6-%E6%8E%A8%E9%80%81%E4%BB%A3%E7%A0%81"><span class="nav-text">13.6 推送代码</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#14-%E6%95%B0%E6%8D%AE%E7%BB%9F%E8%AE%A1"><span class="nav-text">14.数据统计</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#14-1-%E5%88%9B%E5%BB%BA%E5%88%86%E6%94%AF"><span class="nav-text">14.1 创建分支</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#14-2-%E5%88%9B%E5%BB%BA%E8%B7%AF%E7%94%B1"><span class="nav-text">14.2 创建路由</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#14-3-%E5%AF%BC%E5%85%A5ECharts%E5%B9%B6%E4%BD%BF%E7%94%A8"><span class="nav-text">14.3 导入ECharts并使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#14-4-%E6%8E%A8%E9%80%81%E4%BB%A3%E7%A0%81"><span class="nav-text">14.4 推送代码</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#15-%E9%A1%B9%E7%9B%AE%E4%BC%98%E5%8C%96"><span class="nav-text">15.项目优化</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#15-1-%E6%B7%BB%E5%8A%A0%E8%BF%9B%E5%BA%A6%E6%9D%A1"><span class="nav-text">15.1 添加进度条</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#15-2-%E6%A0%B9%E6%8D%AE%E6%8A%A5%E9%94%99%E4%BF%AE%E6%94%B9%E4%BB%A3%E7%A0%81"><span class="nav-text">15.2 根据报错修改代码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#15-3-%E6%89%A7%E8%A1%8Cbuild"><span class="nav-text">15.3 执行build</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#15-4-%E7%94%9F%E6%88%90%E6%89%93%E5%8C%85%E6%8A%A5%E5%91%8A"><span class="nav-text">15.4 生成打包报告</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#15-5-%E4%BF%AE%E6%94%B9webpack%E7%9A%84%E9%BB%98%E8%AE%A4%E9%85%8D%E7%BD%AE"><span class="nav-text">15.5 修改webpack的默认配置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#15-6-%E5%8A%A0%E8%BD%BD%E5%A4%96%E9%83%A8CDN"><span class="nav-text">15.6 加载外部CDN</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#15-7-%E5%AE%9A%E5%88%B6%E9%A6%96%E9%A1%B5%E5%86%85%E5%AE%B9"><span class="nav-text">15.7 定制首页内容</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#15-8-%E8%B7%AF%E7%94%B1%E6%87%92%E5%8A%A0%E8%BD%BD"><span class="nav-text">15.8 路由懒加载</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#16-%E9%A1%B9%E7%9B%AE%E4%B8%8A%E7%BA%BF"><span class="nav-text">16.项目上线</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#16-1-%E9%80%9A%E8%BF%87node%E5%88%9B%E5%BB%BA%E6%9C%8D%E5%8A%A1%E5%99%A8"><span class="nav-text">16.1 通过node创建服务器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#16-2-%E5%BC%80%E5%90%AFgzip%E5%8E%8B%E7%BC%A9"><span class="nav-text">16.2 开启gzip压缩</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#16-3-%E9%85%8D%E7%BD%AEhttps%E6%9C%8D%E5%8A%A1"><span class="nav-text">16.3 配置https服务</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#16-4-%E4%BD%BF%E7%94%A8pm2%E7%AE%A1%E7%90%86%E5%BA%94%E7%94%A8"><span class="nav-text">16.4 使用pm2管理应用</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="hxy"
      src="/images/Robben.gif">
  <p class="site-author-name" itemprop="name">hxy</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">80</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">120</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/huxingyi1997" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;huxingyi1997" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:huxingyi1997@zju.edu.cn" title="E-Mail → mailto:huxingyi1997@zju.edu.cn" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-frog"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">hxy</span>
</div>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共1039.2k字</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'pQsO3ySbU4VtWN2j1FLA74Ha-gzGzoHsz',
      appKey     : 'QYacMDY2VY7Wazprg1X6FiUv',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

  
  <!-- 动态背景特效 -->
  <!-- 樱花特效 -->
    <script async src="/js/src/sakura.js"></script>
    <script async src="/js/src/fairyDustCursor.js"></script>
</body>
</html>
